gpt4 book ai didi

python / Pandas : How to merge rows in dataframe

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:42 25 4
gpt4 key购买 nike

合并两个数据框后:

output = pd.merge(df1, df2, on='ID', how='outer')

我有这样的数据框:

index  x    y   z
0 2 NaN 3
0 NaN 3 3
1 2 NaN 4
1 NaN 3 4
...

如何合并具有相同索引的行?预期输出:

index  x   y  z
0 2 3 3
1 2 3 4

最佳答案

也许,你可以对他们采取刻薄的态度。

In [418]: output.groupby('index', as_index=False).mean()
Out[418]:
index x y z
0 0 2.0 3.0 3
1 1 2.0 3.0 4

关于 python / Pandas : How to merge rows in dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45163159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com