如果两列具有相同的列名,我想合并它们。我希望重复列的值替换NaN值。
我的数据框如下:
PETS PETS FRUITS COUNTRY FRUITS
Pig NaN Apple USA NaN
Cat NaN NaN NL Banana
NaN Dog NaN NaN NaN
这就是我的数据名望:
PETS FRUITS COUNTRY
Pig Apple USA
Cat Banana NL
Dog NaN NaN
答案 0 :(得分:4)
您可以按列分组并使用ffill
后跟pd.concat
:
g = df.groupby(df.columns, axis=1)
res = pd.concat([sub.ffill(axis=1).iloc[:, -1] for _, sub in g], axis=1)
print(res)
COUNTRY FRUITS PETS
0 USA Apple Pig
1 NL Banana Cat
2 NaN NaN Dog
答案 1 :(得分:2)
groupby
first
方法返回第一个非空值。
df.groupby(df.columns, 1).first()
COUNTRY FRUITS PETS
0 USA Apple Pig
1 NL Banana Cat
2 NaN NaN Dog
不进行排序
df.groupby(df.columns, 1, sort=False).first()
PETS FRUITS COUNTRY
0 Pig Apple USA
1 Cat Banana NL
2 Dog NaN NaN