df1.location = 'United Arab Emirates',
'Bahrain',
'Luxembourg',
'Malta',
'Denmark',
'Israel',
'Iceland',
'Russia',
'United States',
'Australia'
df2.location ='Qatar',
'Bahrain',
'Panama',
'Chile',
'San Marino',
'Aruba',
'Kuwait',
'Peru',
'Brazil',
'United States'
```
And i have to find the number of matching elements between two pandas dataframes.
Assuming df1
and df2
to be:
df1 = pd.DataFrame({'location': ['United Arab Emirates', 'Bahrain', 'Luxembourg', 'Malta', 'Denmark', 'Israel', 'Iceland', 'Russia', 'United States', 'Australia']})
df2 = pd.DataFrame({'location': ['Qatar', 'Bahrain', 'Panama', 'Chile', 'San Marino', 'Aruba', 'Kuwait', 'Peru', 'Brazil', 'United States']})
If you want the number of elements matching on the same row/index:
df1['location'].eq(df2['location']).sum()
output: 1
(only row index 1 matches)
If you want the number of matching unique elements, independently of their position:
len(set(df1['location'])&set(df2['location']))
output: 2
('Bahrain' and 'United States' are common)