1 I spy with my little eye
One of the ways you are shown for loading World Bank data into the notebook in Week 7, was to use the download () function.
One way to find out for yourself what sorts of argument a function expects is to ask it. Running a code cell containing a question mark (?) followed by a function name should pop up a help area in the bottom of the notebook window. (Close it using the x in the top right hand corner of the panel.)
In []:
from pandas.io.wb import download
?download
The function documentation tells you that you can enter a list of one or more country names using standard country codes as well as a date range. You can also calculate a date range from a single date to show the N years of data leading up to a particular year.
Note that if you are using the CoCalc free plan, you will not be able to use the download () function to download the data directly from the World Bank API, although you will still be able to inspect the documentation associated with the function.
In []:
YEAR = 2013
GDP_INDICATOR = 'NY.GDP.MKTP.CD'
gdp = download(indicator=GDP_INDICATOR, country=['GB','CN'], start=YEAR-5, end=YEAR)
gdp = gdp.reset_index()
gdp
Out[]:
country | year | NY.GDP.MKTP.CD | |
---|---|---|---|
0 | China | 2013 | 9.490603e+12 |
1 | China | 2012 | 8.461623e+12 |
2 | China | 2011 | 7.492432e+12 |
3 | China | 2010 | 6.039659e+12 |
4 | China | 2009 | 5.059420e+12 |
5 | China | 2008 | 4.558431e+12 |
6 | United Kingdom | 2013 | 2.678173e+12 |
7 | United Kingdom | 2012 | 2.614946e+12 |
8 | United Kingdom | 2011 | 2.592016e+12 |
9 | United Kingdom | 2010 | 2.407857e+12 |
10 | United Kingdom | 2009 | 2.308995e+12 |
11 | United Kingdom | 2008 | 2.791682e+12 |
Although many datasets that you are likely to work with are published in the form of a single data table, such as a single CSV file or spreadsheet worksheet, it is often possible to regard the dataset as being made up from several distinct subsets of data.
In the above example, you will probably notice that each country name appears in several rows, as does each year. This suggests that we can make different sorts of comparisons between different groupings of data using just this dataset. For example, compare the total GDP of each country calculated over the six years 2008 to 2013 using just a single line of code:
In []:
gdp.groupby('country')['NY.GDP.MKTP.CD'].aggregate(sum)
Out[]:
country
China 4.110217e+13
United Kingdom 1.539367e+13
Name: NY.GDP.MKTP.CD, dtype: float64
Essentially what this does is to say ‘for each country, find the total GDP’.
The total combined GDP for those two countries in each year could be found by making just one slight tweak to our code (can you see below where I made the change?):
In []:
gdp.groupby('year')['NY.GDP.MKTP.CD'].aggregate(sum)
Out[]:
year
2008 7.350113e+12
2009 7.368415e+12
2010 8.447515e+12
2011 1.008445e+13
2012 1.107657e+13
2013 1.216878e+13
Name: NY.GDP.MKTP.CD, dtype: float64
That second calculation probably doesn’t make much sense in this particular case, but what if there was another column saying which region of the world each country was in? Then, by taking the data for all the countries in the world, the total GDP could be found for each region by grouping on both the year and the region.
Next, you will consider ways of grouping data.