简体   繁体   中英

Combine 2 different dataframes and getting only the same data

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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM