pandas 内部合并/联接返回所有行 [英] Pandas inner merge/join returning all rows
本文介绍了 pandas 内部合并/联接返回所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图基于两个字段中存在的列合并两个数据帧,仅保留两个集合的交集.
I'm trying to merge two data frames based on a column present in both, keeping only the intersection of the two sets.
所需的结果是:
foo bar foobar
x y z x j i x y z j i
a 1 2 a 9 0 a 1 2 9 0
b 3 4 b 9 0 b 3 4 9 0
c 5 6 c 9 0 c 5 6 9 0
d 7 8 e 9 0
f 9 0
我无法产生预期结果的代码是:
My code that does not produce the desired result is:
pd.merge(foo, bar, how='inner', on='x')
相反,代码似乎返回:
foo bar foobar
x y z x j i x y z j i
a 1 2 a 9 0 a 1 2 9 0
b 3 4 b 9 0 b 3 4 9 0
c 5 6 c 9 0 c 5 6 9 0
d 7 8 e 9 0 e * * 9 0
f 9 0 f * * 9 0
(其中*表示NaN)
我要去哪里错了?我已经到达第三页Google页面,试图解决此问题.无论我做什么,我都会得到一个外部联接,并且两组都包含所有行.
Where am I going wrong? I've already reached the third Google page trying to fix this an nothing works. Whatever I do I get an outer join, with all rows in both sets.
推荐答案
通常,这意味着您在用于连接的列中存在重复项,从而产生了笛卡尔积.
Usually it means that you have duplicates in the column(s) used for joining, resulting in cartesian product.
演示:
In [35]: foo
Out[35]:
x y z
0 a 1 2
1 b 3 4
2 c 5 6
3 d 7 8
In [36]: bar
Out[36]:
x j i
0 a 9 0
1 b 9 0
2 a 9 0
3 a 9 0
4 b 9 0
In [37]: pd.merge(foo, bar)
Out[37]:
x y z j i
0 a 1 2 9 0
1 a 1 2 9 0
2 a 1 2 9 0
3 b 3 4 9 0
4 b 3 4 9 0
这篇关于 pandas 内部合并/联接返回所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文