简体   繁体   中英

How do I match two columns and merge pandas dataframes on Python?

I am trying to match two columns (ie: values from Col1 and Col2 from Df1 match with ColA and Col2 from Df2) and do a merge on that. I'm having trouble with the merge syntax, I currently have merged_df = df1.merge(df2,how='inner',left_on=['Col1,Col2'],right_on=['ColA,Col2]) but it doesn't seem to be working (it's giving me less than what I'm expecting). Any tips?

在此处输入图像描述

Just as you tried, it returns the correct results:

df1.merge(df2, how='inner',left_on=['Col1','Col2'], right_on=['ColA','Col2'])

合并的_df

It's an inner join (keeping only the matches) between both datasets, based on the available couples of ['Col1','Col2'] and ['ColA','Col2'] . The matches are on rows 1 to 6 (or 0 to 5) in both datasets.

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