In this post, I’ll explore electricity access, i.e. globally what fraction of people have access to electricity. Beyond the goal of finding the electricity access, this post will also serve to illustrate how the coolness coefficient of the Python visualisation ecosystem!
I’ll be using data from World Bank for electricity access. See the image below for the corresponding page.
Downloading World Bank data
Now, a Python package called wbdata provides a fairly easy way to access World Bank data. I’d be using it to get data in Pandas DataFrame.
import geopandas as gpdgdf = gpd.read_file('ne_10m_admin_0_countries_lakes.shp')[['ADM0_A3', 'geometry']]
Code
gdf.head()
ADM0_A3
geometry
0
IDN
(POLYGON ((117.7036079039552 4.163414542001791...
1
MYS
(POLYGON ((117.7036079039552 4.163414542001791...
2
CHL
(POLYGON ((-69.51008875199994 -17.506588197999...
3
BOL
POLYGON ((-69.51008875199994 -17.5065881979999...
4
PER
(POLYGON ((-69.51008875199994 -17.506588197999...
Visualising electricity access in 2016
Getting electricity access data for 2016
Code
df_2016 = df_elec.unstack()[['2016']].dropna()
Code
df_2016.head()
date
2016
country
Afghanistan
84.137138
Albania
100.000000
Algeria
99.439568
Andorra
100.000000
Angola
40.520607
In order to visualise electricity access data over the map, we would have to join the GeoPandas object gdf and df_elec
Joining gdf and df_2016
Now, gdf uses alpha_3 codes for country names like AFG, etc., whereas df_2016 uses country names. We will thus use pycountry package to get code names corresponding to countries in df_2016 as shown in this StackOverflow post.
Code
import pycountrycountries = {}for country in pycountry.countries: countries[country.name] = country.alpha_3codes = [countries.get(country, 'Unknown code') for country in df_2016.index]df_2016['Code'] = codes