Sorting, Ranking, and Reindexing
Data rarely comes in the perfect order for analysis. That's where sorting, ranking, and reindexing come in.
Whether you want to sort by column values or manually change the order of rows, pandas makes it simple.
Sorting Values
You can sort a DataFrame based on one or more columns using .sort_values()
.
Sort by Score
import pandas as pd
# Create a DataFrame
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Score": [85, 90, 78]
}
df = pd.DataFrame(data)
# Sort by Score in descending order
df.sort_values(by="Score", ascending=False)
# Output:
# Name Score
# 1 Bob 90
# 0 Alice 85
# 2 Charlie 78
This helps you find top performers, earliest dates, or lowest prices.
Ranking Data
To rank values, use .rank()
. It assigns a rank number to each value and handles ties automatically.
Rank Scores
df["Score"].rank(ascending=False)
# Output:
# 0 2.0
# 1 1.0
# 2 3.0
# Name: Score, dtype: float64
This is useful for leaderboards or percentile-based groupings.
Reindexing Rows
Reindexing lets you reset or customize the order of rows with .reindex()
.
Reorder Rows by Index
df.reindex([2, 0, 1])
# Output:
# Name Score
# 2 Charlie 78
# 0 Alice 85
# 1 Bob 90
Use this when aligning datasets or creating custom views.
Summary
Feature | Method | Purpose |
---|---|---|
Sort Rows | sort_values() | Order rows by column values |
Assign Rankings | rank() | Give rank numbers to values |
Reorder Rows | reindex() | Set a custom row index order |
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.