Examples

In [1]:
import numpy as np
import pandas as pd
In [3]:
df = pd.DataFrame(data={'Animal': ['lion', 'fox', 'cow',
                                   'spider', 'snake'],
                        'Number_legs': [4, 4, 4, 8, np.nan]})
df
Out[3]:
Animal Number_legs
0 lion 4.0
1 fox 4.0
2 cow 4.0
3 spider 8.0
4 snake NaN

The following example shows how the method behaves with the above parameters:

  • default_rank: this is the default behaviour obtained without using any parameter.
  • max_rank: setting method = 'max' the records that have the same values are ranked using
    the highest rank (e.g.: since ‘lion’ and ‘cow’ are both in the 2nd and 3rd position, rank 3 is assigned.)
  • NA_bottom: choosing na_option = 'bottom', if there are records with NaN values they are placed
    at the bottom of the ranking.
  • pct_rank: when setting pct = True, the ranking is expressed as percentile rank.
In [4]:
df['default_rank'] = df['Number_legs'].rank()
In [5]:
df['max_rank'] = df['Number_legs'].rank(method='max')
In [6]:
df['NA_bottom'] = df['Number_legs'].rank(na_option='bottom')
In [7]:
df['pct_rank'] = df['Number_legs'].rank(pct=True)
df
Out[7]:
Animal Number_legs default_rank max_rank NA_bottom pct_rank
0 lion 4.0 2.0 3.0 2.0 0.5
1 fox 4.0 2.0 3.0 2.0 0.5
2 cow 4.0 2.0 3.0 2.0 0.5
3 spider 8.0 4.0 4.0 4.0 1.0
4 snake NaN NaN NaN 5.0 NaN