Ranking Data in Pandas DataFrames with the rank() Method

Overview

The rank() method in pandas assigns ordinal ranks to DataFrame values based on their order. This functionality proves essential when analyzing relative positions within datasets, whether for competitive analysis, performance scoring, or understanding data distribution patterns.

Core Functionality

Basic Ranking

The rank() method evaluates numeric data across columns by default. Missing values and duplicate values receive appropriate handling without additional configuration.

import pandas as pd

scores = {'Math': [85, 92, 92, 78, 88, 95], 'Science': [90, 82, 82, 91, 91, 88]}
result = pd.DataFrame(scores)

ranked = result.rank()
print(ranked)

Output:

   Math  Science
0  1.0     4.0
1  2.0     1.0
2  2.0     1.0
3  3.0     5.0
4  4.0     5.0
5  5.0     6.0

The method returns a new DataFrame where each value is replaced by its corresponding rank position within that column.

Duplicate Value Handling

When identical values appear, the default behavior assigns the mean of all positions those values would occupy. In the example above, both 92 values in the Math column receive rank 2.0 because that's the average of positions 2 and 3.

The method Parameter

This parameter determines how ties are resolved:

Method Behavior
average Default; assigns mean rank to tied values
min Assigns minimum rank among tied values
max Assigns maximum rank among tied values
first Assigns ranks based on appearance order; 1, 2, 2, 4 gets ranks 1, 2, 2, 4
dense Like min, but next rank increases by exactly 1 regardless of ties

Example using method='min':

ranked_min = result.rank(method='min')
print(ranked_min)

Output:

   Math  Science
0  1.0     4.0
1  2.0     1.0
2  2.0     1.0
3  3.0     5.0
4  4.0     5.0
5  5.0     6.0

With min, the fourth and fifth occurrences of 88 receive ranks 4 and 5 rather than both receiving 3.5.

Additional Parameters

ascending

Controls sort direction. Default is True (smallest value gets rank 1). Set to False for reverse ranking:

ranked_desc = result.rank(ascending=False)
print(ranked_desc)

Output:

   Math  Science
0  1.0   2.0
1  3.5   6.0
2  3.5   6.0
3  2.0   1.0
4  2.0   1.0
5  1.0   3.0

axis

By default, ranking occurs along columns (axis=0). Use axis=1 to rank within each row:

row_ranks = result.rank(axis=1)
print(row_ranks)

Output:

   Math  Science
0     1       1
1     2       1
2     2       1
3     1       1
4     1       1
5     2       1

Since row values are unique in this dataset, all ranks are 1 or 2.

numeric_only

When a DataFrame contains non-numeric columns (strings, dates), setting numeric_only=True restricts ranking to numeric colums only, preventing type errors.

na_option

Controls how NaN values are ranked:

  • keep (default): NaN receives highest rank
  • top: NaN receives the lowest rank
  • bottom: NaN receives the highest rank (equivalent to keep)

pct Parameter

Setting pct=True returns percentile ranks instead of integer ranks, useful for statistical analysis:

percentile_ranks = result.rank(pct=True)
print(percentile_ranks)

Output:

   Math  Science
0  0.166667  0.666667
1  0.333333  0.166667
2  0.333333  0.166667
3  0.500000  0.833333
4  0.666667  0.833333
5  0.833333  0.500000

key Parameter

Allows passing a custom function that transforms values before ranking:

ranked_offset = result.rank(key=lambda x: x + 10)
print(ranked_offset)

This offsets all values by 10 before determining ranks, affecting the relative positioning without changing the rank structure itself.

Tags: Pandas DataFrame ranking python data-analysis

Posted on Sun, 13 Sep 2026 16:36:17 +0000 by hykc