0

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'"
martineau
  • 119,623
  • 25
  • 170
  • 301
jpank
  • 13
  • 3
  • I'm not familiar with the `pycountry-convert` module, but the typical way to handle a situation like this in Python would be to first check the active list and if that fails, then try the other one. Since an exception if being raised you should be able to do this by wrapping calls to the module in `try`/`except` clauses. You can put the logic that does all this in your own function to better encapsulate it and make doing it more seamless. – martineau Sep 27 '20 at 22:34
  • After a quick look, it appears that pycountry-convert bases itself off of pycountry and the `pycountry` module only has a very limited database of `historic_countries` - the country you're looking for isn't in that short list. So although it may be worth understanding and fixing your issue on its own merit, you may end up not having a solution that works. – Grismar Sep 27 '20 at 22:42

1 Answers1

0

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.

#!pip install pycountry-convert
from pycountry_convert import country_alpha2_to_continent_code,country_name_to_country_alpha2
import pycountry

def country_2_continent(country_name):
  country_exists=pycountry.countries.get(name=country_name)
  if country_exists is not None:
    country_alpha2=country_name_to_country_alpha2(country_name,cn_name_format="default")
    continent=country_alpha2_to_continent_code(country_alpha2)
  else:
    continent='Unknown'
  return continent
Patricia
  • 71
  • 1
  • 2