Sorting and Ranking
Sorting in pandas is the process of reordering the rows of a DataFrame or Series so the values follow a chosen order, while ranking assigns each value a position number relative to the others.
Learn Sorting and Ranking in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Learn how to order data by one or many columns, control where missing values land, sort by the index, and turn values into competition-style rankings.
The workhorse for ordering data is df.sort_values("column") . It returns a new DataFrame with the rows rearranged so the chosen column is in ascending order by default. The index travels with each row, so you can always see where a row originally came from.
Pass a list to by=["a", "b"] to sort by several columns at once. Pandas sorts by the first column, then breaks ties using the second, and so on. You can give each column its own direction by passing a list of booleans to ascending .
Missing values ( NaN ) are handled by the na_position argument, which defaults to "last" . Set it to "first" to push missing rows to the top instead.
Sometimes you want to order by the index labels rather than the data. That is what df.sort_index() does. After a value sort the index often looks jumbled, so calling reset_index(drop=True) renumbers the rows 0, 1, 2, ... for a clean look.
Series.rank() assigns each value its position relative to the others. The method argument decides how ties are handled. method="min" gives tied values the lowest available rank and leaves a gap afterwards. method="dense" also gives ties the same rank but never skips numbers, so ranks step up by exactly 1.
Replace the blank so the products are sorted by sales from highest to lowest. The first printed row should be the "Laptop" with 920 sales.
Build a leaderboard: sort students by score (high to low), add a dense rank column, and renumber the rows.
Lesson complete — you can order any dataset!
You can now sort by one or many columns, flip the direction, decide where missing values go, sort by the index, and turn values into rankings. These are the building blocks for every report and leaderboard you will ever make.
🚀 Up next: apply, map & applymap — transform your data with custom functions.
Practice quiz
Which method sorts a DataFrame by the data in a column?
- df.sort_values('col')
- df.order('col')
- df.arrange('col')
- df.sortby('col')
Answer: df.sort_values('col'). sort_values('col') orders rows by that column's values.
What is the default sort direction of sort_values()?
- Descending
- Ascending
- Random
- By index
Answer: Ascending. By default ascending=True, so values sort smallest to largest.
How do you sort from largest to smallest?
- sort_values('v', reverse=True)
- sort_values('v', desc=True)
- sort_values('v', ascending=False)
- sort_values('v', order='down')
Answer: sort_values('v', ascending=False). Pass ascending=False to sort in descending order.
Which method sorts by the row labels instead of values?
- df.sort_labels()
- df.sort_keys()
- df.reindex()
- df.sort_index()
Answer: df.sort_index(). sort_index() orders rows by their index labels.
How do you sort by column 'a' then break ties with 'b'?
- a
- b
Answer: a. Pass a list; pandas sorts by 'a' first, then 'b' within ties.
What does sort_values() return by default?
- None
- A new sorted DataFrame, leaving the original unchanged
- Only the sorted column
- A boolean
Answer: A new sorted DataFrame, leaving the original unchanged. It returns a new frame; use inplace=True to modify the original.
Where do NaN values go by default in sort_values()?
- First
- They raise an error
- At the end (last)
- They are dropped
Answer: At the end (last). na_position defaults to 'last', placing NaN at the end.
Which method returns the 3 largest values of a Series directly?
- s.top(3)
- s.maxn(3)
- s.biggest(3)
- s.nlargest(3)
Answer: s.nlargest(3). nlargest(3) returns the three biggest values, already sorted.
How do you put NaN values first when sorting?
- sort_values(na_position='first')
- sort_values(nan='top')
- sort_values(na='start')
- sort_values(missing='first')
Answer: sort_values(na_position='first'). na_position='first' moves NaN values to the top.
Does sort_values() change the original DataFrame's order in place by default?
- No, it returns a sorted copy
- Yes, always in place
- Only for numeric columns
- Only if there are ties
Answer: No, it returns a sorted copy. Without inplace=True it leaves the original untouched and returns a copy.