This should get you close but it will not be 100% accurate. You can use fuzzywuzzy. fuzzywuzzy
uses Levenshtein distance to calculate the difference between two strings:
from fuzzywuzzy import process
# create a choice list
choices = clist['Country'].values.tolist()
# apply fuzzywuzzy to each row using lambda expression
cdata['Close Country'] = cdata['Country'].apply(lambda x: process.extractOne(x, choices)[0])
# merge
cdata.merge(clist, left_on='Close Country', right_on='Country')
Country_x Close Country Country_y BL?
0 Braizl Brazil Brazil No
1 Rusia Russia Russia Yes
2 us US US No
You can even return the percent match and only keep values > n if you want to only keep matches say greater than 85%
add percent match
from fuzzywuzzy import process
# create a choice list
choices = clist['Country'].values.tolist()
# apply fuzzywuzzy to each row using lambda expression
cdata['Close Country'] = cdata['Country'].apply(lambda x: process.extractOne(x, choices))
# add percent match wiht apply
cdata[['Close Country', 'Percent Match']] = cdata['Close Country'].apply(pd.Series)
# merge
cdata.merge(clist, left_on='Close Country', right_on='Country')
Country_x Close Country Percent Match Country_y BL?
0 Braizl Brazil 83 Brazil No
1 Rusia Russia 91 Russia Yes
2 us US 100 US No
you can do boolean indexing either before the merge to remove bad match then merge:
cdata[['Close Country', 'Percent Match']] = cdata['Close Country'].apply(pd.Series)
cdata = cdata[cdata['Percent Match']>85]
or you can do it after the merge:
merge = cdata.merge(clist, left_on='Close Country', right_on='Country')
merge[merge['Percent Match'] > 85]
fuzzywuzzy
returns the percent match as part of the process
function. In the first example I removed it by calling the first element of the tuple: process.extractOne(x, choices)[0]