Learn to code for data analysis
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

Learn to code for data analysis

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

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371