Get Continent Names & Create World Map From Country List With Python

Using World Development Indicators Dataset

Nathan Arianto
3 min readFeb 6, 2023

If we download World Development Indicators (WDI) dataset from DataBank. We will get dataset contains ‘Country Name’ (name of the country), ‘Country Code’ (code that represent to the specific country), ‘Series Name’ (Given name of a series), ‘Series Code’ (a number or a sequence of related statistical units arranged or occurring in temporal spatial or other order or succession), and value that represent the series by year that we choose, as follows:

Install & Import Libraries

First, we need to install libraries needed. ‘pycountry_convert’ will be used to get continent names from country and ‘geopandas’ will be used to create world maps.

!pip install pycountry_convert
!pip install geopandas

import pycountry_convert as pc
import geopandas

Country to Continent

Using ‘pycountry_convert’ we will convert country alpha 3 codes that we have in the dataset to alpha 2. After that, we will get continent code from alpha 2 code and then continent name from continent code.

In WDI dataset, not everything is country names. There are some region names. So, the last line from snippet below is used for that reason.

def country_to_continent(country_code):
while True:
try:
country_alpha2 = pc.country_alpha3_to_country_alpha2(country_code)
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
except:
return country_code

#Make continent column from country code
df['Continent'] = df['Country Code'].apply(lambda x: country_to_continent(x))
df['Continent'] = np.where(df['Continent'] == df['Country Code'], df['Country Name'], df['Continent'])

Table below is the result after running the function above. I will only use ‘Access to electricity (% of population)’ value on 2019 for map color.

Create World Map using GeoPandas

To create world map, we need to merge our data with GeoPandas data. GeoPandas data contain estimate population, continent, country name, alpha 3 codes, GDP, and geometry data.

Merge above data with our dataset. To do that we might need to rename column names. In the code below, we change ‘iso_a3’ to ‘Country Code’ so that it’s the same as dataset that we have. After that, we can plot our world map.

can define the plot axes (with ax) and the legend axes (with cax) and then pass those in to the plot() call. The following example uses ‘mpl_toolkits’ to vertically align the plot axes and the legend axes

# Merge geopandas data with our data, so we can make the map visualization
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
# Rename the columns so that we can merge with our data
world.columns=['pop_est', 'continent2', 'name', 'Country Code', 'gdp_md_est', 'geometry']

# Merge with our data
map=pd.merge(world,df,on='Country Code')


# Plot world map
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, ax = plt.subplots(figsize=(20,20))
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.1)
map.plot(column='2019 [YR2019]', ax=ax, legend=True,cax=cax,colormap="YlOrRd_r", edgecolor='black')
plt.title('Access to electricity (% of population)',x=-10,fontsize=30)
plt.show()

--

--