Skip to main content
Practice

Sorting, Ranking, and Reindexing

Data rarely comes in the perfect order for analysis — that’s where sorting and reindexing come in. Whether you want to sort by column values or change row order manually, Pandas makes it simple.


Sorting Values

You can sort a DataFrame based on one or more columns using .sort_values().

Sort by Score
df.sort_values(by="Score", ascending=False)

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, handling ties gracefully.

Rank Scores
df["Score"].rank(ascending=False)

This is great for leaderboards or percentile-based groupings.


Reindexing Rows

Reindexing lets you reset or customize the order of your DataFrame’s rows with .reindex().

Reorder Rows by Index
df.reindex([2, 0, 1])

Use this when aligning datasets or creating custom views.


Summary Table

FeatureMethodPurpose
Sort Rowssort_values()Order rows by column values
Assign Rankingsrank()Give rank numbers to values
Reorder Rowsreindex()Set a custom row index order

What’s Next?

You’ll now work with missing and duplicate data to keep your datasets clean and reliable.