Grabbing World Bank Data with the wbdata module and plotting it

by Tariq Khokhar

In [15]:
#I'm running pylab but if you're not, don't forget to import pandas and matplotlib too!

import wbdata

Let's start by setting the countries and indicators we want - in this case, I'm after the GNI per capita for Chile, Uruguay and Hungary.

In [2]:
countries = ["CL","UY","HU"]
In [3]:
indicators = {'NY.GNP.PCAP.CD':'GNI per Capita'}

OK, we now use wbdata's "get_dataframe" function to grab the data and put it into a pandas dataframe

In [4]:
df = wbdata.get_dataframe(indicators, country=countries, convert_date=False)

Let's just check what we've got there

In [5]:
GNI per Capita
country date
Chile 2012 14280
2011 12270
2010 10720
2009 9940
2008 10020
In [6]:
GNI per Capita
country date
Uruguay 1964 660
1963 610
1962 580
1961 NaN
1960 NaN

OK, that looks like the data we want but it's currently "pivoted" which is no good for plotting. Luckily, the pandas unstack() function has us covered:

In [7]:
dfu = df.unstack(level=0)
In [8]:
GNI per Capita
country Chile Hungary Uruguay
1960 NaN NaN NaN
1961 NaN NaN NaN
1962 600 NaN 580
1963 640 NaN 610
1964 660 NaN 660

That's ready to plot, we could get rid of the missing values with dropna() but let's leave it for now - a one-liner with matplotlib should get us there:

In [12]:
plt.figure(); dfu.plot(); plt.legend(loc='best'); plt.title("GNI Per Capita ($USD, Atlas Method)"); plt.xlabel('Date'); plt.ylabel('GNI Per Capita ($USD, Atlas Method');
<matplotlib.figure.Figure at 0x922cdd0>

You can spend more time styling your plot than I did - matplotlib's documentation is good and you can even go all XKCD on your charts!

In [ ]: