How to use the rank function on a dataframe in pandas

What is a dataframe?

A dataframe is a commonly used 2-dimensional data structure. It is a table with columns and rows and is used primarily as a pandas object.

A sample dataframe

It requires the pandas library, as shown below.

import pandas as pd

A dataframe can be formed as shown below. The following is a dataframe that contains countries that have been put in different groups and are given different a_scores and b_scoresBoth the scores are imaginary values for this example.

import pandas as pd
a_score = [4, 5, 7, 8, 2, 3, 1, 6, 9, 10]
b_score = [1, 2, 3, 4, 5, 6, 7, 10, 8, 9]
country = ['Pakistan', 'USA', 'Canada', 'Brazil', 'India', 'Beligium', 'Malaysia', 'Peru', 'England', 'Scotland']
groups = ['A','A','B','A','B','B','C','A','C','C']
df = pd.DataFrame({'group':groups, 'country':country, 'a_score':a_score, 'b_score':b_score})
print(df)

Rank function

The rank function assigns ranks to values. For [13,5,11,9], the assigned ranks would be [4,1,3,2]. 5 is the smallest number, so it gets ranked 1, followed by 9, which is bigger than 5, so it gets 2, and so on.

Prototype

The function prototype is as follows.

df['ranked'] = df['a_score'].rank()

Parameter

The values you want to rank are the parameters.

Return value

The respective ranks are returned.

Example

The following example assigns ranks to a_score values.

import pandas as pd
a_score = [4, 5, 7, 18, 12, 13, 1, 16, 9, 10]
b_score = [1, 2, 3, 4, 5, 6, 7, 10, 8, 9]
country = ['Pakistan', 'USA', 'Canada', 'Brazil', 'India', 'Beligium', 'Malaysia', 'Peru', 'England', 'Scotland']
groups = ['A','A','B','A','B','B','C','A','C','C']
df = pd.DataFrame({'group':groups, 'country':country, 'a_score':a_score, 'b_score':b_score})
df['ranked'] = df['a_score'].rank()
print(df)

Free Resources