Informatics Practices · Ch 3 — Data Handling using Pandas – II
Sorting a DataFrame
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) …
| Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|
| Ashravy | 1 | 23 | 19 | 20 | 15 | 22 |
| Ashravy | 2 | 24 | 22 | 24 | 17 | 21 |
| Ashravy | 3 | 12 | 25 | 19 | 21 | 23 |
| Mishti | 1 | 15 | 22 | 25 | 22 | 22 |
| Mishti | 2 | 18 | 21 | 25 | 24 | 23 |
| Mishti | 3 | 17 | 18 | 20 | 25 | 20 |
| Raman | 1 | 22 | 21 | 18 | 20 | 21 |
| Raman | 2 | 21 | 20 | 17 | 22 | 24 |
| Raman | 3 | 14 | 19 | 15 | 24 | 23 |
| Zuhaire | 1 | 20 | 17 | 22 | 24 | 19 |
| Zuhaire | 2 | 23 | 15 | 21 | 25 | 15 |
| Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|
| Zuhaire | 2 | 23 | 15 | 21 | 25 | 15 |
| Raman | 2 | 21 | 20 | 17 | 22 | 24 |
| Mishti | 2 | 18 | 21 | 25 | 24 | 23 |
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 …
| Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|
| Zuhaire | 3 | 22 | 18 | 19 | 23 | 13 |
| Mishti | 3 | 17 | 18 | 20 | 25 | 20 |
| Raman | 3 | 14 | 19 | 15 | 24 | 23 |
| Ashravy | 3 | 12 | 25 | 19 | 21 | 23 |