YHJ
YHJ

Reputation: 11

Python Intersection of two Dataframe

I need to intersect data frame. So I executed the following code. But I cannot get the result I want.

df1 = pd.Dataframe([
      ['psy', 180, 75],
      ['psy', 180, 75]
], columns = ['name', 'height', 'weight'])

df2 = pd.Dataframe([
      ['psy', 180, 75],
      ['df', 160, 65],
      ['ddqq', 170, 75],
      ['psy', 180, 75]
], columns = ['name', 'height', 'weight'])

print(pd.merge(df1, df2, how='inner'))

Output

  name  height  weight
0 psy   180     75
1 psy   180     75
2 psy   180     75
3 psy   180     75

But I want the below results.

 name  height  weight
0 psy   180     75
1 psy   180     75

How can I get the following result? Please Tell me...

Upvotes: 1

Views: 2442

Answers (1)

Derek O
Derek O

Reputation: 19647

You can drop duplicates from one of the DataFrames. For example:

pd.merge(df1, df2.drop_duplicates(), how='inner')

Output:

  name  height  weight
0  psy     180      75
1  psy     180      75

Upvotes: 1

Related Questions