Home > Software design >  How to join data frames with keeping only matching ones?
How to join data frames with keeping only matching ones?

Time:11-10

I have two dataframes:

id   value
a1   12
a2   15
a3   17
id   value1
a1   22
a2   58
a7   10

I want to join them by column id keeping only matching one:

id   value    value1
a1    12       22
a2    15       58

How to do that? I tried pd.merge but it keeps all id

CodePudding user response:

You can use pd.merge and inner:

Example of dataframe

df1 = pd.DataFrame({'id':['a1','a2', 'a3'],
                    'value':[12, 15, 17]})

df2 = pd.DataFrame({'id':['a1','a2', 'a7'],
                    'value':[22, 58, 10]})

code

merged_df = df1.merge(df2, how = 'inner', on = ['id'])
merged_df.columns = ["id", "value", "value1"]

Result

   id  value  value1
0  a1     12      22
1  a2     15      58

If you want to change index as id, you can use pd.set_index('id')

  • Related