pandas 合并删除重复行 [英] Pandas merge removing duplicate rows
本文介绍了 pandas 合并删除重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一只熊猫 df:
df = pd.DataFrame({'id':[1,1,2,2,3],
'type':['a','b','c','d','e'],
'value':[100,200,300,400,500]})
print(df)
id value type
1 100 a
1 200 b
2 300 c
2 400 d
3 500 e
我正在合并相同的数据帧以获得
I'am merging the same dataframe to get combinations of
df2 = pd.merge(df, df,on=['id'])
print(df2)
id type_x value_x type_y value_y
1 a 100 a 100
1 a 100 b 200
1 b 200 a 100
1 b 200 b 200
2 c 300 c 300
2 c 300 d 400
2 d 400 c 300
2 d 400 d 400
3 e 500 e 500
但我不想要带有 value_x = value_y
例如:
id type_x value_x type_y value_y
1 a 100 a 100
合并后可以选择列
df2 = df2[df2.value_x != df2.value_y]
但我不想这样做,
有没有其他方法可以在合并时删除它们?
is there any other way, by which i can remove these while merging itself?
我的最终输出(期望):
my final output (desired):
id type_x value_x type_y value_y
1 a 100 b 200
1 b 200 a 100
2 c 300 d 400
2 d 400 c 300
推荐答案
您可以在一个语句中完成所有操作,但是,它仍然很像您正在执行的操作,使用 query
.
You can do it all in one statement, however, it is still much like you are doing, using query
.
df2 = pd.merge(df, df,on=['id']).query('value_x != value_y')
输出:
id type_x value_x type_y value_y
1 1 a 100 b 200
2 1 b 200 a 100
5 2 c 300 d 400
6 2 d 400 c 300
这篇关于 pandas 合并删除重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文