How could I convert country name to continent name in a list of countries, some currently in existence and others no longer existing (ex. USSR)?
I have a table of data from countries around the world spanning the last fifty years. I want to add a new column that mimics the column of listed countries and then converts the country name into its corresponding continent.
I've made a function using pycountry_convert
but it only can take input of countries currently in existence. There is a pycountry tool that handles "historical" names of countries that no longer exist (ex: USSR) but the list doesn't include countries in existence. I think my country_to_continent()
function needs the ability to test inputs against either list, active or inactive countries, but I'm not sure what that would look like.
# country_to_continent takes country_name, converts it to an alpha code and then
# looks up the continent using the alpha code.
def country_to_continent(country_name):
country_alpha2 = pc.country_name_to_country_alpha2(country_name)
country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)
country_continent_name = pc.convert_continent_code_to_continent_name(country_continent_code)
return country_continent_name
#create new column
hivdata.insert(1,"column1", np.nan)
#new column replicates first column indexing by country name
hivdata["column1"] = hivdata['Estimated HIV Prevalence% - (Ages 15-49)']
#apply function to column 1
hivdata['column1'] = hivdata['column1'].apply(country_to_continent)
Error:
70 if cn_name not in dict_country_name_to_country_alpha2:
---> 71 raise KeyError("Invalid Country Name: '{0}'".format(cn_name))
72
73 return dict_country_name_to_country_alpha2[cn_name]
KeyError: "Invalid Country Name: 'Abkhazia'"
I check first if the country name is in the list of countries that can be transformed and the ones that I don't have information I return as continent
Unknown
.