Learn to code for data analysis

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

Free course

# 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 []:

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 = gdp.reset_index()

gdp

Out[]:

countryyearNY.GDP.MKTP.CD
0China20139.490603e+12
1China20128.461623e+12
2China20117.492432e+12
3China20106.039659e+12
4China20095.059420e+12
5China20084.558431e+12
6United Kingdom20132.678173e+12
7United Kingdom20122.614946e+12
8United Kingdom20112.592016e+12
9United Kingdom20102.407857e+12
10United Kingdom20092.308995e+12
11United Kingdom20082.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.

LCDAB_1