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

In [16]:
country_geo = r'/Users/Harish/Documents/HK_Work/Python/world-development-indicators/world-countries.json'

In [17]:
# Read in the World Development Indicators Database
data = pd.read_csv(r'/Users/Harish/Documents/HK_Work/Python/world-development-indicators/Indicators.csv')
data.shape


Out[17]:
(5656458, 6)

In [18]:
data.head()


Out[18]:
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 [19]:
# 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[19]:
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 [20]:
plot_data = stage[['CountryCode','Value']]
plot_data.head()


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

In [21]:
# 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 [22]:
# 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 [23]:
# choropleth maps bind Pandas Data Frames and json geometries.  This allows us to quickly visualize data combinations
map.choropleth(geo_data=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)


---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-23-b4c711a4291f> in <module>()
      4              key_on='feature.id',
      5              fill_color='YlGnBu', fill_opacity=0.7, line_opacity=0.2,
----> 6              legend_name=hist_indicator)

/anaconda/lib/python3.6/site-packages/folium/folium.py in choropleth(self, geo_data, data, columns, key_on, threshold_scale, fill_color, fill_opacity, line_color, line_weight, line_opacity, name, legend_name, topojson, reset, smooth_factor, highlight)
    325                 style_function=style_function,
    326                 smooth_factor=smooth_factor,
--> 327                 highlight_function=highlight_function if highlight else None)
    328 
    329         self.add_child(geo_json)

/anaconda/lib/python3.6/site-packages/folium/features.py in __init__(self, data, style_function, name, overlay, control, smooth_factor, highlight_function)
    480             else:  # This is a filename
    481                 with open(data) as f:
--> 482                     self.data = json.loads(f.read())
    483         elif data.__class__.__name__ in ['GeoDataFrame', 'GeoSeries']:
    484             self.embed = True

/anaconda/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

/anaconda/lib/python3.6/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

/anaconda/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 7 column 1 (char 6)

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

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


Out[13]:

In [ ]: