Ranking Values
Assign ranks to column values with rank(), choose tie-breaking methods, and create percentile bins with pd.cut().
What Is Ranking?
Ranking assigns each value in a Series a position based on its relative magnitude — rank 1 for the smallest value, rank n for the largest (or vice versa). Unlike sorting (which reorders rows), rank() adds a new column of ordinal positions alongside the original data. Rankings are used in leaderboards, percentile computation, and certain statistical tests that require ordinal position rather than absolute values.
import pandas as pd
df = pd.DataFrame({
'player': ['Alice', 'Bob', 'Carol', 'Dave', 'Eve'],
'score': [88, 72, 95, 72, 85]
})
df['rank'] = df['score'].rank()
print(df)
# player score rank
# 0 Alice 88 4.0
# 1 Bob 72 1.5 <- tied with Dave
# 2 Carol 95 5.0
# 3 Dave 72 1.5 <- tied with Bob
# 4 Eve 85 3.0Ascending vs Descending Rank
By default, rank() gives rank 1 to the smallest value (ascending). To give rank 1 to the largest (e.g., first place in a competition), pass ascending=False. This mirrors the convention of 'first place = highest score' used in sports, leaderboards, and sales rankings.
import pandas as pd
df = pd.DataFrame({'score': [70, 95, 85, 60, 90]})
# Rank 1 = highest score
df['competition_rank'] = df['score'].rank(ascending=False)
print(df)
# score competition_rank
# 0 70 4.0
# 1 95 1.0
# 2 85 3.0
# 3 60 5.0
# 4 90 2.0