Pandas: How to Calculate Rank in a GroupBy Object


You can use the following syntax to calculate the rank of values in a GroupBy object in pandas:

df['rank'] = df.groupby(['group_var'])['value_var'].rank()

The following example shows how to use this syntax in practice.

Example: Calculate Rank in a GroupBy Object

Suppose we have the following pandas DataFrame that shows the points scored by basketball players on various teams:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'],
                   'points': [10, 10, 12, 15, 19, 23, 20, 20, 26]})

#view DataFrame
print(df)

  team  points
0    A      10
1    A      10
2    A      12
3    A      15
4    B      19
5    B      23
6    C      20
7    C      20
8    C      26

We can use the following syntax to calculate the rank of the points values for each team:

#add ranking column to data frame
df['points_rank'] = df.groupby(['team'])['points'].rank()

#view updated DataFrame
print(df)

  team  points  points_rank
0    A      10          1.5
1    A      10          1.5
2    A      12          3.0
3    A      15          4.0
4    B      19          1.0
5    B      23          2.0
6    C      20          1.5
7    C      20          1.5
8    C      26          3.0

By default, the rank() function assigns ranking values in ascending order and uses the average rank when ties are present.

However, we can use the method and ascending arguments to rank the values in a different manner:

#add ranking column to data frame
df['points_rank'] = df.groupby(['team'])['points'].rank('dense', ascending=False)

#view updated DataFrame
print(df)

  team  points  points_rank
0    A      10          3.0
1    A      10          3.0
2    A      12          2.0
3    A      15          1.0
4    B      19          2.0
5    B      23          1.0
6    C      20          2.0
7    C      20          2.0
8    C      26          1.0

This method assigns a value of 1 to the largest value in each group.

You can find a complete list of ranking methods you can use with the rank() function here.

Note: You can find the complete documentation for the GroupBy operation in pandas here.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Pandas: How to Calculate Cumulative Sum by Group
Pandas: How to Count Unique Values by Group
Pandas: How to Calculate Correlation By Group

Leave a Reply

Your email address will not be published.