Skip to content Skip to sidebar Skip to footer

Pandas Inner Merge/join Returning All Rows

I'm trying to merge two data frames based on a column present in both, keeping only the intersection of the two sets. The desired result is: foo bar foobar x

Solution 1:

Usually it means that you have duplicates in the column(s) used for joining, resulting in cartesian product.

Demo:

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

Post a Comment for "Pandas Inner Merge/join Returning All Rows"