Pandas: Rank Numerical Data with rank()

Compute numerical rankings for values along an axis using the Pandas rank() method. Handle tied values using tie-breaking strategies (average, min, max, first, dense), rank in ascending or descending order, and compute percentile ranks.

Introduction to rank() 🔝

The rank() method computes numerical ranks (1 through N) for elements in a Series or DataFrame column. By default, smaller values receive lower rank positions (ascending order), and tied values receive their average rank position.

Create numerical sample dataset with ties 🔝

We start with a student score dataset containing identical tied scores to demonstrate tie-breaking strategies.
import pandas as pd

df = pd.DataFrame({
    "STUDENT": ["Ravi", "Raju", "Alex", "John", "Sara"],
    "SCORE": [80, 90, 80, 95, 70]
})
print(df)
assert df.shape == (5, 2)

Expected output

  STUDENT  SCORE
0    Ravi     80
1    Raju     90
2    Alex     80
3    John     95
4    Sara     70

Basic ranking (ascending vs descending) 🔝

Use ascending=False to rank from highest score (Rank 1) to lowest score.
df["RANK_DESC"] = df["SCORE"].rank(ascending=False)
print(df)
assert df.loc[3, "RANK_DESC"] == 1.0

Expected output

  STUDENT  SCORE  RANK_DESC
0    Ravi     80        3.5
1    Raju     90        2.0
2    Alex     80        3.5
3    John     95        1.0
4    Sara     70        5.0

Tie-breaking methods (method parameter) 🔝

Pandas supports 5 distinct tie-breaking methods:
  • "average": Default. Assigns average rank to tied items (e.g., 3.5).
  • "min": Assigns lowest rank of the group to all tied items.
  • "max": Assigns highest rank of the group to all tied items.
  • "first": Ranks tied items based on order of appearance in the data.
  • "dense": Like "min", but rank always increases by 1 for the next group (no gaps).
df["MIN_RANK"] = df["SCORE"].rank(ascending=False, method="min")
df["DENSE_RANK"] = df["SCORE"].rank(ascending=False, method="dense")
print(df[["STUDENT", "SCORE", "MIN_RANK", "DENSE_RANK"]])
assert df.loc[0, "MIN_RANK"] == 3.0 and df.loc[4, "DENSE_RANK"] == 4.0

Expected output

  STUDENT  SCORE  MIN_RANK  DENSE_RANK
0    Ravi     80       3.0         3.0
1    Raju     90       2.0         2.0
2    Alex     80       3.0         3.0
3    John     95       1.0         1.0
4    Sara     70       5.0         4.0

Percentile ranking (pct=True) 🔝

Pass pct=True to rescale values between 0.0 and 1.0, returning the relative percentile rank.
df["PCT_RANK"] = df["SCORE"].rank(pct=True)
print(df[["STUDENT", "SCORE", "PCT_RANK"]])
assert df.loc[3, "PCT_RANK"] == 1.0

Expected output

  STUDENT  SCORE  PCT_RANK
0    Ravi     80       0.5
1    Raju     90       0.8
2    Alex     80       0.5
3    John     95       1.0
4    Sara     70       0.2

Exercise: rank sales leaders 🔝

Given a Series of sales figures sales = pd.Series([500, 700, 500, 900], name="SALES"), compute a descending rank using method="min". Verify that the highest sales figure receives Rank 1.0.

Exercise solution 🔝

Solution creating the Series and applying rank().
sales = pd.Series([500, 700, 500, 900], name="SALES")
answer = sales.rank(ascending=False, method="min")
print(answer)
assert answer[3] == 1.0

Expected output

0    3.0
1    2.0
2    3.0
3    1.0
Name: SALES, dtype: float64

Practice in Google Colab 🔝

Interactive Colab Notebook

Practice rank() examples interactively directly in your browser:

Open In Colab View on GitHub

Reference: Pandas rank() documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer