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