The country converter (coco) is a Python package to convert country names into different classifications and between different naming versions. Internally it uses regular expressions to match country names.
The package is available as PyPI, use
pip install country_converter -upgrade
from the command line or use your preferred python package installer. The sourcecode is available on github: https://github.com/konstantinstadler/country_converter
The country converter provides one main class which is used for the conversion:
In [1]:
import country_converter as coco
In [2]:
converter = coco.CountryConverter()
Given a list of countries is a certain classification:
In [3]:
iso3_codes = ['USA', 'VUT', 'TKL', 'AUT', 'AFG', 'ALB']
This can be converted to any classification provided by:
In [4]:
converter.convert(names = iso3_codes, src = 'ISO3', to = 'name_official')
Out[4]:
or
In [5]:
converter.convert(names = iso3_codes, src = 'ISO3', to = 'continent')
Out[5]:
The parameter "src" specifies the input-, "to" the output format. Possible values for both parameter can be found by:
In [6]:
converter.valid_class
Out[6]:
Internally, these names are the column header of the underlying pandas dataframe (see below).
The convert function can also be accessed without initiating the CountryConverter. This can be useful for one time usage. For multiple matchings, initiating the CountryCotnverter avoids that the file providing the matching data gets read in for each conversion.
In [7]:
converter.convert(names = iso3_codes, src = 'ISO3', to = 'ISO2')
Out[7]:
Some of the classifications can be accessed by some shortcuts. For example:
In [8]:
converter.EU27
Out[8]:
In [9]:
converter.OECDas('ISO2')
Out[9]:
The return value for non-found entries is be default set to 'not found':
In [10]:
iso3_codes_missing = ['ABC', 'AUT', 'XXX']
converter.convert(iso3_codes_missing, src='ISO3')
Out[10]:
but can also be rest to something else:
In [11]:
converter.convert(iso3_codes_missing, src='ISO3', not_found='missing')
Out[11]:
Alternativly, the non-found entries can be passed through by passing None to not_found:
In [12]:
converter.convert(iso3_codes_missing, src='ISO3', not_found=None)
Out[12]:
To extend the underlying dataset, an additional dataframe (or file) can be passed.
In [13]:
import pandas as pd
add_data = pd.DataFrame.from_dict({
'name_short' : ['xxx country', 'abc country'],
'name_official' : ['The XXX country', 'The ABC country'],
'regex' : ['xxx country', 'abc country'],
'ISO3': ['xxx', 'abc']}
)
In [14]:
add_data
Out[14]:
In [15]:
extended_converter = coco.CountryConverter(additional_data=add_data)
extended_converter.convert(iso3_codes_missing, src='ISO3', to='name_short')
Out[15]:
Alternatively to a ad hoc dataframe, additional datafiles can be passed. These must have the same format as basic data set. An example can be found here: https://github.com/konstantinstadler/country_converter/tree/master/tests/custom_data_example.txt
The custom data example contains the ISO3 code mapping for Romania before 2002 and switches the regex matching for congo between DR Congo and Congo Republic.
To use is pass the path to the additional country file:
In [16]:
# extended_converter = coco.CountryConverter(additional_data=path/to/datafile)
The passed data (file or dataframe) must at least contain the headers 'name_official', 'name_short' and 'regex'. Of course, if the additional data shall be used to a conversion to any other field, these must also be included.
Additionally passed data always overwrites the existing one. This can be used to adjust coco for datasets with wrong country names. For example, assuming a dataset erroneous switched the ISO2 codes for India (IN) and Indonesia (ID) (therefore assuming 'ID' for India and 'IN' for Indonesia), one can accomedate for that by:
In [17]:
switched_converter = coco.CountryConverter(additional_data=pd.DataFrame.from_dict({
'name_short' : ['India', 'Indonesia'],
'name_official' : ['India', 'Indonesia'],
'regex' : ['india', 'indonesia'],
'ISO2': ['ID', 'IN']}))
In [18]:
converter.convert('IN', src='ISO2', to='name_short')
Out[18]:
In [19]:
switched_converter.convert('ID', src='ISO2', to='name_short')
Out[19]:
The input parameter "src" can be set to "regex" to use regular expression matching for a given country list. For example:
In [20]:
some_names = ['United Rep. of Tanzania', 'Cape Verde', 'Burma', 'Iran (Islamic Republic of)', 'Korea, Republic of', "Dem. People's Rep. of Korea"]
In [21]:
coco.convert(names = some_names, src = "regex", to = "name_short")
Out[21]:
The regular expressions can also be used to match any list of countries to any other. For example:
In [22]:
match_these = ['norway', 'united_states', 'china', 'taiwan']
master_list = ['USA', 'The Swedish Kingdom', 'Norway is a Kingdom too', 'Peoples Republic of China', 'Republic of China' ]
coco.match(match_these, master_list)
Out[22]:
If the regular expression matches several times, all results are given as list and a warning is generated:
In [23]:
match_these = ['norway', 'united_states', 'china', 'taiwan']
master_list = ['USA', 'The Swedish Kingdom', 'Norway is a Kingdom too', 'Peoples Republic of China', 'Taiwan, province of china', 'Republic of China' ]
coco.match(match_these, master_list)
Out[23]:
The parameter "enforce_sublist" can be set to ensure consistent output:
In [24]:
coco.match(match_these, master_list, enforce_sublist = True)
Out[24]:
A warning also ococours if one of the names couldn't be found:
In [25]:
match_these = ['norway', 'united_states', 'china', 'taiwan', 'some other country']
master_list = ['USA', 'The Swedish Kingdom', 'Norway is a Kingdom too', 'Peoples Republic of China', 'Republic of China' ]
coco.match(match_these, master_list)
Out[25]:
And the value for non found countries can be specified:
In [26]:
coco.match(match_these, master_list, not_found = 'its not there')
Out[26]:
This can also be used to pass the not found country to the new classification:
In [27]:
coco.match(match_these, master_list, not_found = None)
Out[27]:
Within the new instance, the raw data for the conversion is saved within a pandas dataframe. This dataframe can be acocoessed directly with:
In [28]:
converter.data.head()
Out[28]:
This dataframe can be extended in both directions. The only requirement is to provide unique values for name_short, name_official and regex.
Internally, the data is saved in country_data.txt as tab-separated values (utf-8 encoded).
Of course, all pandas indexing and matching methods can be used. For example, to get new OECD members since 1995 present in a list:
In [29]:
some_countries = ['Australia', 'Belgium', 'Brazil', 'Bulgaria', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'India', 'Indonesia', 'Ireland', 'Italy', 'Japan', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Romania', 'Russia', 'Turkey', 'United Kingdom', 'United States']
converter.data[(converter.data.OECD >= 1995) & converter.data.name_short.isin(some_countries)].name_short
Out[29]:
Further information can be found here: http://pandas.pydata.org/pandas-docs/stable/
All regular expressions of the country converter are tested for a unique match to name_short and name_official. Test sets for alternative names found in various databases are also available.
The test sets are stored in the /test subbolder. To tests require py.test. I recommend to rerun the test if a regular expression is changed.
To specify a new test set just add a tab-separated file with headers "name_short" and "name_test" and provide name (corresponding to the short name in the main classification file) and the alternative name which should be tested (one pair per row in the file). If the file name starts with "test_regex_ " it will be automatically recognised by the test functions.
Please see the file CONTRIBUTING.rst for further information.
Konstantin Stadler
In [ ]: