pees Slur
pees Slur

Reputation: 33

How to stop pandas merge() function to do cartesian product when key values are repeated?

a = pd.DataFrame({"a": [1,2], "b": [1,1]})
b = pd.DataFrame({"c": [3,4], "b": [1,1]})
c = a.merge(b, on=['b'], how='inner')

we will get c having 4 lines. But how can we get c like {'a':[1,2], 'c':[3,4], 'b':[1,1]} by using merge() function?

Upvotes: 3

Views: 134

Answers (1)

JongHyeon Yeo
JongHyeon Yeo

Reputation: 1039

This could work. Pandas merge is not that good when you implement it.

c = pd.concat([a, b], axis=1, join='inner')
c = c.loc[:,~c.columns.duplicated()]
print(c)

returns this:

   a  b  c
0  1  1  3
1  2  1  4

Upvotes: 1

Related Questions