Skip to content

Informatics Practices · Ch 3 — Data Handling using Pandas – II

Sorting a DataFrame

3.4

Sorting a DataFrame

Sorting means arranging data elements in a specified order — either ascending (smallest to largest) or descending (largest to smallest). In Pandas, the sort_values() function is used to sort the rows or columns of a DataFrame.

The general syntax is:

DataFrame.sort_values(by, axis=0, ascending=True)

Here, by is a list of column names (or a single column name) on which to sort. The axis argument tells Pandas whether to sort along rows (axis=0) or along columns (axis=1). The ascending parameter controls the order: True for ascending, False for descending. By default, sorting is done on row indexes in ascending order.

Consider a teacher who wants to arrange a list of students alphabetically by name, or by marks in a particular subject. Sorting makes this easy.

Example 1: Sorting the entire DataFrame by the 'Name' column

print(df.sort_values(by=['Name']))

This sorts all rows in ascending order of student names. The output shows Raman, Zuhaire, Ashravy, and Mishti arranged alphabetically (with each student's three unit test rows grouped together).

Example 2: Sorting a filtered subset — marks in Science for Unit Test 2

First, filter the DataFrame to keep only rows where UT == 2:

dfUT2 = df[df.UT == 2]

Then sort this filtered data by marks in Science:

print(dfUT2.sort_values(by=['Science']))

The output lists students in ascending order of their Science marks in Unit Test 2: Zuhaire (15), Raman (20), Mishti (21), Ashravy (22).

Example 3: Sorting in descending order — English marks for Unit Test 3

Filter for Unit Test 3, then sort by 'Eng' in descending order:

dfUT3 = df[df.UT == 3]
print(dfUT3.sort_values(by=['Eng'], ascending=False))

The output shows Raman (23), Ashravy (23), Mishti (20), Zuhaire (13) — highest English marks first.

Sorting by multiple columns

A DataFrame can be sorted based on more than one column. When two rows have the same value in the first column, the second column is used as a tiebreaker.

For example, to sort Unit Test 3 data by Science marks (ascending), and for students with equal Science marks, sort by Hindi marks (ascending):

dfUT3 = df[df.UT == 3]
print(dfUT3.sort_values(by=['Science', 'Hindi']))

The output shows:

  • Zuhaire (Science 18, Hindi 23)
  • Mishti (Science 18, Hindi 25)
  • Raman (Science 19, Hindi 24)
  • Ashravy (Science 25, Hindi 21) …
Table 3.24Output of df.sort_values(by=['Name'])
NameUTMathsScienceS.StHindiEng
Ashravy12319201522
Ashravy22422241721
Ashravy31225192123
Mishti11522252222
Mishti21821252423
Mishti31718202520
Raman12221182021
Raman22120172224
Raman31419152423
Zuhaire12017222419
Zuhaire22315212515
Table 3.25Output of dfUT2.sort_values(by=['Science'])
NameUTMathsScienceS.StHindiEng
Zuhaire22315212515
Raman22120172224
Mishti21821252423
DefinitionProgram 3-9

Write the statement which will sort the marks in English in the DataFrame df based on Unit Test 3, in descending order. This worked example demonstrates sort_values() with ascending=False, filter …

Table 3.26Output of dfUT3.sort_values(by=['Science','Hindi']) -- multi-column sort
NameUTMathsScienceS.StHindiEng
Zuhaire32218192313
Mishti31718202520
Raman31419152423
Ashravy31225192123