-1

I want to combine 2 dataframes and create another dataframe with combined values that have the id in common.

DF1:

id    country   age

123    US       30

234    MY       45

432    AF       32

DF2:

id   country   name

123    US      Bill

234    MY      Luis

432    AF      Joe

787    SG      Mark

How do I combine these two df and get only the data with same 'id'.

What i want:

DF3:

id    country   age   name 

123    US       30    Bill

234    MY       45    Luis

432    AF       32    Joe
Sociopath
  • 13,068
  • 19
  • 47
  • 75
nam
  • 116
  • 1
  • 10

1 Answers1

1

Take a look at this Join pandas dataframes based on column values.

You can do

df3 = pd.merge(df1, df2, on=['id','country'], how='left')
Shubham Sharma
  • 714
  • 1
  • 8
  • 18
  • I tried this on my data, I have about 2500 on one df and about 10,000 on the other df. I was supposed to get 2500 or lower but I got about 9000 for the merged df. – nam Jan 29 '20 at 08:14
  • 1
    Basically I used your suggestion and added: `df3.drop_duplicates(subset ="id", keep = "first", inplace = True) ` and got the results I wanted. Thanks! – nam Jan 29 '20 at 08:17
  • Check my edit, you can use the left join. [Reference](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge.html) – Shubham Sharma Jan 29 '20 at 08:18