Matching Country Information in Python | by Joe T. Santhanavanich | Towards Data Science

In data science projects, you may have a data table that contains country data like country name, country code, etc. But, different data sets usually provide data in different country data formats or attributes; for example, “Germany” can be recorded as “GER”, “Germany”, “DE”, “Federal Republic of Germany”, country code — 276, etc.). This would cause you some difficulties to join tables, data aggregation, data analysis, or data visualization.
This article introduces PyCountry, a Python library for completing the country data from the country database to ready your dataset to be matched or aggregated to others.
Let’s get started~
You can install this library using pip as the following command:
$ pip install pycountry
First, you can create a blank Python file and import the pycountry library.
import pycountry
Then, you can get any information from each country from the following attributes:
Name (e.g. Thailand)
Alpha 2 code (e.g. “TH”)
Alpha 3 code (e.g. “THA”)
Numeric code (e.g. “764”)
Official name (e.g. “Kingdom of Thailand”)
For example, if you have “Alpha 2 code” and you can use countries.get command to get all information:
pycountry.countries.get(alpha_2='TH')>>> Country(alpha_2='TH', alpha_3='THA', name='Thailand', numeric='764', official_name='Kingdom of Thailand')
If you want only the nameoutput, you can do it in a single command as follows:
pycountry.countries.get(alpha_2='TH').name>>> 'Thailand'
The PyCountry library also comes with a “fuzzy” search to help you to find countries without input their types. For example:
pycountry.countries.search_fuzzy('Thailand')>>> [Country(alpha_2='TH', alpha_3='THA', name='Thailand', numeric='764', official_name='Kingdom of Thailand')]
That’s about it. Now, let’s head to try this library in the real-world use case.
Complete country columns in the COVID-19 case report dataset from CSSE.
First, we will use Python to load a raw COVID-19 case dataset from this URL and load it into the Pandas dataframe, as shown in the following scripts. After loading the dataset, you can see that it contains the country name in the “Country_Region” column.
import pycountryimport pandas as pddf = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/12-18-2020.csv')
Then, we can use the apply method to add country information from the PyCountry database to this dataframe. We can also define the PyCountry countries search as a function to avoid the error in case it does not found the input country name.
The following script shows how to apply the “Alpha 2 Code” to the whole dataframe. You can also do the same to add other attributes to the dataframe.
def findCountry (country_name):    try:        return pycountry.countries.get(name=country_name).alpha_2    except:        return ("not founded")df['country_alpha_2'] = df.apply(lambda row: findCountry(row.Country_Region) , axis = 1)
With these add country columns, we can use them as primary keys to aggregated with other data sets easily.
Please Find it here:
This article gives a short overview of how to get country information using the PyCountry library which can help you to merge data with other datasets easily.
I hope you like this article and found it useful for your daily work or projects. Feel free to leave me a message if you have any questions or comments.
About me & Check out all my blog contents: Link
Be Safe and Healthy! 💪
Thank you for Reading. 📚