0
0
Pandasdata~5 mins

rank() method and ranking methods in Pandas

Choose your learning style9 modes available
Introduction

The rank() method helps us find the position of values in a list or column, like giving scores a place in a race.

When you want to know the order of students based on their test scores.
When you need to rank products by sales to find the best sellers.
When comparing athletes' times to see who came first, second, etc.
When sorting data but want to keep the original order and just add ranks.
When handling ties and deciding how to assign ranks to equal values.
Syntax
Pandas
DataFrame.rank(method='average', ascending=True, na_option='keep', numeric_only=None, pct=False)

method decides how to handle ties (equal values).

ascending=True means smaller values get better (lower) ranks.

Examples
Ranks the scores with default method 'average' and ascending order.
Pandas
df['score'].rank()
Ties get the smallest rank among them.
Pandas
df['score'].rank(method='min')
Ranks in descending order, ties get the largest rank.
Pandas
df['score'].rank(method='max', ascending=False)
Ranks are shown as percentage of the total count.
Pandas
df['score'].rank(pct=True)
Sample Program

This code creates a table of names and scores. It then adds four new columns showing different ways to rank the scores:

  • rank_average: average rank for ties
  • rank_min: smallest rank for ties
  • rank_max_desc: largest rank for ties, ranking from highest to lowest score
  • rank_pct: rank as a percentage of total entries
Pandas
import pandas as pd

data = {'name': ['Anna', 'Bob', 'Cara', 'Dan', 'Eva'],
        'score': [88, 92, 88, 95, 92]}
df = pd.DataFrame(data)

# Rank scores with default method (average)
df['rank_average'] = df['score'].rank()

# Rank scores with method 'min'
df['rank_min'] = df['score'].rank(method='min')

# Rank scores with method 'max' and descending order
df['rank_max_desc'] = df['score'].rank(method='max', ascending=False)

# Rank scores as percentage
df['rank_pct'] = df['score'].rank(pct=True)

print(df)
OutputSuccess
Important Notes

The method options are:

  • average: average rank for ties (default)
  • min: lowest rank for ties
  • max: highest rank for ties
  • first: ranks in order they appear
  • dense: like min but ranks increase by 1 only

Use ascending=False to rank from highest to lowest values.

Ranking with pct=True gives ranks as fractions between 0 and 1.

Summary

rank() helps find the position of values in data.

You can choose how to handle ties with different method options.

Ranking can be done in ascending or descending order, and as percentages.