0

I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ?

df1

place name qty unit
NY    Tom   2  10
TK    Ron   3  15
Lon   Don   5  90
Hk    Sam   4  49

df2

place name price 
PH    Tom   7    
TK    Ron   5    

Result:

df3

place name qty unit
NY    Tom   2  10
TK    Ron   3  15

2 Answers 2

0

Option 1

Using df.isin:

In [1362]: df1[df1.name.isin(df2.name)]
Out[1362]: 
  place name  qty  unit
0    NY  Tom    2    10
1    TK  Ron    3    15

Option 2

Performing an inner-join with df.merge:

In [1365]: df1.merge(df2.name.to_frame())
Out[1365]: 
  place name  qty  unit
0    NY  Tom    2    10
1    TK  Ron    3    15

Option 3

Using df.eq:

In [1374]: df1[df1.name.eq(df2.name)]
Out[1374]: 
  place name  qty  unit
0    NY  Tom    2    10
1    TK  Ron    3    15
Sign up to request clarification or add additional context in comments.

Comments

0

You want something called an inner join.

df1.merge(df2,on = 'name')

place_x name    qty unit place_y price
NY      Tom      2  10     PH      7
TK      Ron      3  15     TK      5

The _xand _y happens when you have a column in both data frames being merged.

2 Comments

I've already covered this in my answer, and besides, this has more columns than what OP wants.
@cᴏʟᴅsᴘᴇᴇᴅ I'm leaving it up in case it is helpful to another user. If OP finds your answer most helpful, he can accept it.

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.