In [1]:
import sys                             # system module
import pandas as pd                    # data package
import matplotlib.pyplot as plt        # graphics module  
import datetime as dt                  # date and time module
import numpy as np                     # foundation for Pandas
import seaborn.apionly as sns          # fancy matplotlib graphics (no styling)
from pandas.io import data, wb         # worldbank data
import plotly.plotly as py             # importing stuff to Plotly
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets

# plotly imports
from plotly.offline import iplot, iplot_mpl  # plotting functions
import plotly.graph_objs as go               # ditto
import plotly                                # just to print version and init notebook
import cufflinks as cf                       # gives us df.iplot that feels like df.plot
cf.set_config_file(offline=True, offline_show_link=False)

# these lines make our graphics show up in the notebook
%matplotlib inline             
plotly.offline.init_notebook_mode()

# check versions (overkill, but why not?)
print('Python version:', sys.version)
print('Pandas version: ', pd.__version__)
print('Plotly version: ', plotly.__version__)
print('Today: ', dt.date.today())


/Users/jeffreyhu/anaconda/lib/python3.5/site-packages/pandas/io/data.py:33: FutureWarning: 
The pandas.io.data module is moved to a separate package (pandas-datareader) and will be removed from pandas in a future version.
After installing the pandas-datareader package (https://github.com/pydata/pandas-datareader), you can change the import ``from pandas.io import data, wb`` to ``from pandas_datareader import data, wb``.
  FutureWarning)
/Users/jeffreyhu/anaconda/lib/python3.5/site-packages/pandas/io/wb.py:19: FutureWarning: 
The pandas.io.wb module is moved to a separate package (pandas-datareader) and will be removed from pandas in a future version.
After installing the pandas-datareader package (https://github.com/pydata/pandas-datareader), you can change the import ``from pandas.io import data, wb`` to ``from pandas_datareader import data, wb``.
  FutureWarning)
Python version: 3.5.1 |Anaconda 2.4.1 (x86_64)| (default, Dec  7 2015, 11:24:55) 
[GCC 4.2.1 (Apple Inc. build 5577)]
Pandas version:  0.17.1
Plotly version:  1.9.10
Today:  2016-05-12

In [2]:
gini = wb.download(country= 'all',indicator = ['SI.POV.GINI'],start=1990, end=2013)

In [3]:
url = "http://unstats.un.org/unsd/methods/m49/m49alpha.htm"
iso = pd.read_html(url, attrs={"border": "0", "cellpadding": "2"}, header=0)[0]
iso = iso.rename(columns={"ISO ALPHA-3 code": "ISO", 
                          "Country or area name": "country"})
iso = iso.drop("Numerical  code", axis=1)
iso.head()


Out[3]:
country ISO
0 Afghanistan AFG
1 Åland Islands ALA
2 Albania ALB
3 Algeria DZA
4 American Samoa ASM

In [4]:
iso['country']=iso['country'].replace('United States of America','United States')
iso[-15:]


Out[4]:
country ISO
226 United Arab Emirates ARE
227 United Kingdom of Great Britain and Northern I... GBR
228 United Republic of Tanzania TZA
229 United States USA
230 United States Virgin Islands VIR
231 Uruguay URY
232 Uzbekistan UZB
233 Vanuatu VUT
234 Venezuela (Bolivarian Republic of) VEN
235 Viet Nam VNM
236 Wallis and Futuna Islands WLF
237 Western Sahara ESH
238 Yemen YEM
239 Zambia ZMB
240 Zimbabwe ZWE

In [8]:
gini1 = gini.reset_index()
gini1.head(5)


Out[8]:
country year SI.POV.GINI
0 Arab World 2013 NaN
1 Arab World 2012 NaN
2 Arab World 2011 NaN
3 Arab World 2010 NaN
4 Arab World 2009 NaN

In [9]:
gini2 = pd.merge(gini1,iso,how='left',on='country')

In [10]:
gini2.head(5)


Out[10]:
country year SI.POV.GINI ISO
0 Arab World 2013 NaN NaN
1 Arab World 2012 NaN NaN
2 Arab World 2011 NaN NaN
3 Arab World 2010 NaN NaN
4 Arab World 2009 NaN NaN

In [11]:
gini2 = gini2.set_index(['ISO','year'])
gini2.columns = ['Country','Gini']

In [12]:
gini2 = gini2.reset_index()

In [13]:
gini2.head(5)


Out[13]:
ISO year Country Gini
0 NaN 2013 Arab World NaN
1 NaN 2012 Arab World NaN
2 NaN 2011 Arab World NaN
3 NaN 2010 Arab World NaN
4 NaN 2009 Arab World NaN

In [14]:
gini3 = gini2.pivot_table(values = 'Gini', index = 'ISO',columns = 'year')
gini3.head(5)
gini4 = gini3.copy()

In [15]:
gini5 = gini4.T.fillna(method='pad').T

In [16]:
gini5.head(5)


Out[16]:
year 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 ... 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
ISO
ABW NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
AFG NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
AGO NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 51.96 51.96 51.96 51.96 42.72 42.72 42.72 42.72 42.72 42.72
ALB NaN NaN NaN NaN NaN NaN 27.01 27.01 27.01 27.01 ... 32.46 30.60 30.60 30.60 29.98 29.98 29.98 29.98 28.96 28.96
AND NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

5 rows × 24 columns


In [17]:
gini6 = gini5.reset_index()

In [23]:
gini7 = pd.merge(gini6,iso,how='left',on='ISO')

In [24]:
gini7 = gini7.set_index('ISO')

In [25]:
gini7.head(5)


Out[25]:
year 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 ... 2005 2006 2007 2008 2009 2010 2011 2012 2013 country
ISO
ABW NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN Aruba
AFG NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN Afghanistan
AGO NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 51.96 51.96 51.96 42.72 42.72 42.72 42.72 42.72 42.72 Angola
ALB NaN NaN NaN NaN NaN NaN 27.01 27.01 27.01 27.01 ... 30.60 30.60 30.60 29.98 29.98 29.98 29.98 28.96 28.96 Albania
AND NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN Andorra

5 rows × 25 columns


In [26]:
years = list(range(1990,2011))

In [30]:
scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.3, 'rgb(188,189,220)'],
            [0.4, 'rgb(158,154,200)'],[0.6, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]

def map_gini(year):
    trace = dict(type="choropleth",
                 locations=list(gini5.index),   # use ISO names
                 z=gini7[str(year)], # defines the color
                 colorscale= scl,    # change pallette
                 text=gini7['country'],         # change text on hover
                 )

    layout = dict(geo=dict(
            showframe = False,
            showcoastlines = True), 
                  width=900, height=700)

    # reuse the same layout
    iplot(go.Figure(data=[trace], layout=layout), link_text="")


interact(map_gini, year=widgets.IntSlider(min=1993,max=2011,step=1));



In [ ]:


In [ ]:


In [ ]: