How to compare column values in different dataframes?

Viewed 55

I have found this code , and it is working very well.

df1 = pd.DataFrame({'c1': [1, 4, 7], 'c2': [2, 5, 1], 'c3': [3, 1, 1]})
 df2 = pd.DataFrame({'c4': [1, 4, 7], 'c2': [3, 5, 2], 'c3': [3, 7, 5]})
 set(df1['c2']).intersection(set(df2['c2']))

But I need to compare them with multiple column names like

set(df1['c2']['c3']).intersection(set(df2['c2']['c3']))

But it is not working. How to fix or what is another way to compare them and find similar values ( matches ) ?

1 Answers

You can try with merge follow by drop_duplicates

df1.merge(df2,on = ['c2','c3'])[['c2','c3']].drop_duplicates()
Related