Data Source: https://www.kaggle.com/worldbank/world-development-indicators
Folder: 'world-development-indicators'

Using Folium Library for Geographic Overlays

Further exploring CO2 Emissions per capita in the World Development Indicators Dataset


In [1]:
import folium
import pandas as pd


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-6e0d97fb4c63> in <module>()
----> 1 import folium
      2 import pandas as pd

ModuleNotFoundError: No module named 'folium'

In [2]:
country_geo = 'geo/world-countries.json'

In [3]:
# Read in the World Development Indicators Database
data = pd.read_csv('world-development-indicators/Indicators.csv')
data.shape


Out[3]:
(5656458, 6)

In [4]:
data.head()


Out[4]:
CountryName CountryCode IndicatorName IndicatorCode Year Value
0 Arab World ARB Adolescent fertility rate (births per 1,000 wo... SP.ADO.TFRT 1960 1.335609e+02
1 Arab World ARB Age dependency ratio (% of working-age populat... SP.POP.DPND 1960 8.779760e+01
2 Arab World ARB Age dependency ratio, old (% of working-age po... SP.POP.DPND.OL 1960 6.634579e+00
3 Arab World ARB Age dependency ratio, young (% of working-age ... SP.POP.DPND.YG 1960 8.102333e+01
4 Arab World ARB Arms exports (SIPRI trend indicator values) MS.MIL.XPRT.KD 1960 3.000000e+06

Pull out CO2 emisions for every country in 2011


In [5]:
# select CO2 emissions for all countries in 2011
hist_indicator = 'CO2 emissions \(metric'
hist_year = 2011

mask1 = data['IndicatorName'].str.contains(hist_indicator) 
mask2 = data['Year'].isin([hist_year])

# apply our mask
stage = data[mask1 & mask2]
stage.head()


Out[5]:
CountryName CountryCode IndicatorName IndicatorCode Year Value
5026275 Arab World ARB CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 4.724500
5026788 Caribbean small states CSS CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 9.692960
5027295 Central Europe and the Baltics CEB CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 6.911131
5027870 East Asia & Pacific (all income levels) EAS CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 5.859548
5028456 East Asia & Pacific (developing only) EAP CO2 emissions (metric tons per capita) EN.ATM.CO2E.PC 2011 5.302499

Setup our data for plotting.

Create a data frame with just the country codes and the values we want plotted.


In [6]:
plot_data = stage[['CountryCode','Value']]
plot_data.head()


Out[6]:
CountryCode Value
5026275 ARB 4.724500
5026788 CSS 9.692960
5027295 CEB 6.911131
5027870 EAS 5.859548
5028456 EAP 5.302499

In [7]:
# label for the legend
hist_indicator = stage.iloc[0]['IndicatorName']

Visualize CO2 emissions per capita using Folium

Folium provides interactive maps with the ability to create sophisticated overlays for data visualization


In [8]:
# Setup a folium map at a high-level zoom @Alok - what is the 100,0, doesn't seem like lat long
map = folium.Map(location=[100, 0], zoom_start=1.5)

In [9]:
# choropleth maps bind Pandas Data Frames and json geometries.  This allows us to quickly visualize data combinations
map.choropleth(geo_path=country_geo, data=plot_data,
             columns=['CountryCode', 'Value'],
             key_on='feature.id',
             fill_color='YlGnBu', fill_opacity=0.7, line_opacity=0.2,
             legend_name=hist_indicator)

In [15]:
# Create Folium plot
map.save('plot_data.html')

In [14]:
# Import the Folium interactive html file
from IPython.display import HTML
HTML('<iframe src=plot_data.html width=700 height=450></iframe>')


Out[14]:

In [ ]: