I have a json object called 'countries' like below with all the countries ISO code list:
countries = [{"name":"Afghanistan","alpha-2":"AF","country-code":"004"},{"name":"Åland Islands","alpha-2":"AX","country-code":"248"},{"name":"Albania","alpha-2":"AL","country-code":"008"},{"name":"Algeria","alpha-2":"DZ","country-code":"012"}]
I have a pandas dataframe with 'Country' column:
Country
--------
Albania
Algeria
Algeria
I want to replace the Country column 'name' with the 'alpha-2' value from the json object. The result should be like :
Country
---------
AL
DZ
DZ
I am trying to do something like this which does not give any error nor changes the values.
df['Country'] = df['Country'].replace(lambda y: (x['alpha-2'] for x in countries) if y in (x['name'] for x in countries) else y)