Ranking & Top-N (rank, nlargest)
Ranking assigns each value its position in the ordering — 1st, 2nd, 3rd — while top-N helpers like nlargest pull out just the highest or lowest rows without sorting everything.
Learn Ranking & Top-N (rank, nlargest) in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
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.
You'll learn Series.rank() with its tie methods, nlargest / nsmallest , ranking within groups, and percentile ranks with pct=True .
Series.rank() replaces each value with its ordinal position, smallest first by default. The interesting part is ties , controlled by method= :
When you only want the extremes, df.nlargest(n, "col") returns the n rows with the biggest values in that column, already sorted, and nsmallest does the same for the lowest. It is shorter and faster than fully sorting because pandas only has to locate n items.
Combine groupby with rank to rank inside each group — every team or department restarts at 1. And pct=True turns the rank into a percentile between 0 and 1, useful for comparing standing on a common scale.
By default rank counts up from the lowest value:
Find the two highest earners in each department.
Lesson complete — you can find the winners every time!
You can rank with the right tie method, flip direction for leaderboards, grab extremes with nlargest / nsmallest , rank within groups, and read percentiles with pct=True .
🚀 Up next: Checkpoint — Time Series & I/O — put resample, SQL, and ranking together.
Practice quiz
Which Series method assigns ranks to values?
- s.order()
- s.rank()
- s.sort()
- s.position()
Answer: s.rank(). Series.rank() returns the rank of each value.
What is the DEFAULT method= used by rank()?
- 'average'
- 'min'
- 'first'
- 'dense'
Answer: 'average'. By default rank() uses method='average', averaging ranks of ties.
For [10, 20, 20, 40] with method='min', what is the rank of the two 20s?
- 3 each
- 2.5 each
- 2 each
- 1 each
Answer: 2 each. method='min' gives tied values the lowest rank in the group, so both 20s get 2.
With method='dense' on [10, 20, 20, 40], what rank does 40 get?
- 3
- 4
- 2
- 5
Answer: 3. dense leaves no gaps after ties, so 40 is rank 3.
Which method= breaks ties by order of appearance?
- 'average'
- 'dense'
- 'max'
- 'first'
Answer: 'first'. method='first' assigns ranks in the order the values appear.
How do you rank from highest value to lowest?
- rank(ascending=False)
- rank(reverse=True)
- rank(top=True)
- rank(desc=1)
Answer: rank(ascending=False). Pass ascending=False so the largest value gets rank 1.
For [10, 20, 20, 40] with method='max', what rank do the 20s get?
- 2 each
- 4 each
- 3 each
- 2.5 each
Answer: 3 each. method='max' gives tied values the highest rank in the group, so both 20s get 3.
What does rank(pct=True) return?
- Ranks as integers
- The count of ties
- Sorted values
- Ranks as percentiles between 0 and 1
Answer: Ranks as percentiles between 0 and 1. pct=True expresses each rank as a percentile of the data.
Does rank() change the order of the rows?
- No, it returns ranks aligned to the original index
- Yes, it sorts ascending
- Yes, it sorts descending
- It drops ties first
Answer: No, it returns ranks aligned to the original index. rank() keeps the original order; only the values become ranks.
Which method gives [1, 2.5, 2.5, 4] for [10, 20, 20, 40]?
- method='min'
- method='average'
- method='dense'
- method='first'
Answer: method='average'. The default 'average' averages the tied positions 2 and 3 to 2.5.