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.5 Sorting on a column

One of the research questions was: which countries have the smallest and largest number of deaths?

Being a small table, it is not too difficult to scan the TB deaths column and find those countries. However, such a process is prone to errors and impractical for large tables. It’s much better to sort the table by that column, and then look up the countries in the first and last rows.

As you’ve guessed by now, sorting a table is another single line of code.

In []:

data.sort_values('TB deaths')

Out[]:

CountryPopulation (1000s)TB deaths
9Sao Tome and Principe19318
3Equatorial Guinea75767
7Portugal10608140
11Timor-Leste1133990
4Guinea-Bissau17041200
1Brazil2003624400
0Angola214726900
8Russian Federation14283417000
6Mozambique2583418000
10South Africa5277625000
2China139333741000
5India1252140240000

The dataframe method sort_values() takes as argument a column name and returns a new dataframe where the rows are in ascending order of the values in that column. Note that sorting doesn’t modify the original dataframe.

In []:

data # rows still in original order

Out[]:

CountryPopulation (1000s)TB deaths
0Angola214726900
1Brazil2003624400
2China139333741000
3Equatorial Guinea75767
4Guinea-Bissau17041200
5India1252140240000
6Mozambique2583418000
7Portugal10608140
8Russian Federation14283417000
9Sao Tome and Principe19318
10South Africa5277625000
11Timor-Leste1133990

It’s also possible to sort on a column that has text instead of numbers; the rows will be sorted in alphabetical order.

In []:

data.sort_values('Country')

Out[]:

CountryPopulation (1000s)TB deaths
0Angola214726900
1Brazil2003624400
2China139333741000
3Equatorial Guinea75767
4Guinea-Bissau17041200
5India1252140240000
6Mozambique2583418000
7Portugal10608140
8Russian Federation14283417000
9Sao Tome and Principe19318
10South Africa5277625000
11Timor-Leste1133990

Exercise 8 sorting on a column

Use the Exercise notebook 1 to sort the table by population so that you can quickly see which are the least and the most populous countries. Remember to run all code before doing the exercise.

In the next section you’ll learn about calculations over columns.

LCDAB_1