1

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...

1
  • I think it is because the same name psy for 2 rows pandas is confused how to join. You can try by name=ing them psy1 and psy2 then you will get desired result. Commented Nov 5, 2021 at 4:27

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.