简体   繁体   中英

How to compare Multiple columns in different Dataframes with Pandas

I have two dataframes having 6000 rows and 20 columns. I want to compare these two dataframes on 3 columns so that if the values match then those matched rows go to a new dateframe and if the values do not match then they go to a second new dataframe. For this, I tried to use if statement but it is giving me error that " The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). " and I checked everything online to tackle it but I failed.

In datatype it shows boolean

In if statement, it shows a error

Any help would be appreciated.

For the dataframe where the rows match you can do this with a inner join (rows that match in both dataframes come through into the new dataframe). See below something that should work

joined_df_match = df1.merge(df2, how = 'inner', left_on = ['col1','col2','col3'], right_on = ['col1','col2','col3'])

For when they don't match you could use an outer join

joined_df_non = df1.merge(df2, how = 'outer', left_on = ['col1','col2','col3'], right_on = ['col1','col2','col3'])

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