Sorting Data
Learn how to sort your DataFrame by one or more columns so you can quickly see highest and lowest values and understand rankings in your dataset.
Skill Level: Beginner
Prerequisites: Filtering Data
Estimated Time: 20 minutes
Story Time
You can now filter datasets to focus on specific groups of students.
Often, the next question is about ranking: who scored the highest, who scored the lowest, and how students compare to one another. Sorting lets you see these patterns clearly instead of scanning hundreds of records manually.
In this lesson, you will learn how to sort your DataFrame by one or more columns.
What You’ll Learn
By the end of this lesson, you will be able to:
- Sort a DataFrame by one or more columns.
- Sort data in ascending and descending order.
- Identify the highest and lowest values in a dataset.
- Prepare data for reporting and visualization.
Why This Topic Matters
Imagine your instructor asks:
Which students scored the highest in Mathematics?
or:
Which students have the lowest Reading scores?
Sorting the dataset lets you answer these questions quickly, without manually searching through rows. Sorting helps reveal patterns, rankings, and extreme values that are important in analysis and reporting.[file:42]
Analysis Question
Consider this question:
Who are the top‑performing students in Mathematics?
Sorting the math score column in descending order will help you find the answer.
Sorting a Column
Use sort_values() to sort by a single column:
df.sort_values("math score")
This sorts the DataFrame in ascending order, so the smallest values appear first.
Descending Order
To see the highest scores first, set ascending=False:
df.sort_values(
"math score",
ascending=False
)
Now the rows are ordered from highest math score to lowest.
Sorting Multiple Columns
You can sort by more than one column at a time.
For example, to sort by gender and then by mathematics score:
df.sort_values(
["gender", "math score"]
)
This sorts:
- first by gender, and
- within each gender, by math score in ascending order.
Mixed Sorting
Each column can have its own sorting order:
df.sort_values(
["gender", "math score"],
ascending=[True, False]
)
This sorts:
- gender alphabetically (ascending)
- math score from highest to lowest (descending) within each gender
This is useful when you want a stable group ordering but still highlight top scores inside each group.
Worked Example
Question:
Who are the five highest‑scoring students in Mathematics?
df.sort_values(
"math score",
ascending=False
).head(5)
Question:
Who are the five students with the lowest Reading scores?
df.sort_values(
"reading score"
).head(5)
These examples show how sorting and head() can be combined to find top or bottom values.
Practice in the Notebook
After completing the activity in Google Colab, mark it as complete below.
Using the StudentsPerformance dataset in Colab:[file:42]
- Sort the DataFrame by mathematics score and show the top five students.
- Sort by reading score and show the top five students.
- Sort by writing score and show the top five students.
Write each operation in a separate cell and compare the results.
Practice Exercise
Answer the following questions:
- Which five students have the highest mathematics scores?
- Which five students have the lowest writing scores?
- Sort students alphabetically by gender.
- Sort students by parental level of education.
Use sort_values() for each question and inspect the outputs.
Independent Exercise
A principal wants a list of students ranked by performance.
Sort the dataset by:
- mathematics score
- reading score
- writing score
Compare the three rankings and decide which one you think best represents overall academic performance. Write a short explanation of your reasoning.
Common Mistakes
Forgetting ascending=False
df.sort_values("math score")
shows the lowest scores first. To see the highest scores first, you must specify:
df.sort_values(
"math score",
ascending=False
)
Assuming the Original DataFrame Changes Automatically
sorted_df = df.sort_values("math score")
creates a new sorted DataFrame. The original df remains unchanged unless you assign the result back to df:
df = df.sort_values("math score")
Key Takeaways
In this lesson, you learned:
- How to sort a DataFrame by one or more columns.
- How to use ascending and descending order.
- How sorting reveals rankings and extreme values in a dataset.
Self Evaluation
Check Your Understanding
1. Which command sorts the DataFrame by math score in ascending order?
2. How do you sort math scores in descending order?
3. Which command sorts by gender and then math score?
4. How can you see the top five students in math?
5. Does df.sort_values("math score") change df permanently?
Continue Your Journey
Next, you will learn Creating New Columns, which will let you add derived values such as total scores or averages to your DataFrames.