Rank the dataframe in python pandas – (min, max, dense & rank by group)

In this tutorial we will learn how to rank the dataframe in python pandas by ascending and descending order with maximum rank value, minimum rank value , average rank value and dense rank . We will see an example for each.  We will be ranking the dataframe on row wise on different methods

In this tutorial we will be dealing with following examples

  • Rank the dataframe by ascending and descending order
  • Rank the dataframe by dense rank if found 2 values are same
  • Rank the dataframe by Maximum rank if found 2 values are same
  • Rank the dataframe by Minimum rank if found 2 values are same
  • Rank the dataframe by group

Create data frame:


import pandas as pd
import numpy as np

#Create a DataFrame
d = {
'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine',
'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'],
'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science',
'History','History','History','Economics','Economics','Economics'],
'Score':[62,47,55,74,31,77,85,63,42,62,89,85]}

df = pd.DataFrame(d,columns=['Name','Subject','Score'])
df

Resultant dataframe will be

Rank the dataframe in python pandas – (min, max, dense & rank by group) 1

 

Ranking the dataframe in python pandas on ascending order:

Now lets rank the dataframe in ascending order of score as shown below


# Ranking of score ascending order

df['score_ranked']=df['Score'].rank(ascending=1)
df

so the result will be

Rank the dataframe in python pandas – (min, max, dense & rank by group) 2

 

Ranking the dataframe in python pandas on descending order:

rank the dataframe in descending order of score as shown below


# Ranking of score descending order

df['score_ranked']=df['Score'].rank(ascending=0)
df

so the result will be

Rank the dataframe in python pandas – (min, max, dense & rank by group) 3

 

Rank the dataframe in python pandas by minimum value of the rank

rank the dataframe in descending order of score and if found two scores are same then assign the minimum rank to both the score as shown below


# Ranking of score in descending order by minimum value

df['score_ranked']=df['Score'].rank(ascending=0,method='min')
df

in this example score 62 is found twice and is ranked by minimum value of 7

so the result will be

Rank the dataframe in python pandas – (min, max, dense & rank by group) 4

 

Rank the dataframe in python pandas by maximum value of the rank

rank the dataframe in descending order of score and if found two scores are same then assign the maximum rank to both the score as shown below


# Ranking of score in descending order by maximum value

df['score_ranked']=df['Score'].rank(ascending=0,method='max')
df

In this example score 62 is found twice and is ranked by maximum value of 8

so the result will be

Rank the dataframe in python pandas – (min, max, dense & rank by group) 4

 

Rank the dataframe in python pandas by dense rank

rank the dataframe in descending order of score and if found two scores are same then assign the same rank . Dense rank does not skip any rank (in min and max ranks are skipped)


# Ranking of score in descending order by dense

df['score_ranked']=df['Score'].rank(ascending=0,method='dense')
df

so the result will be

Rank the dataframe in python pandas – (min, max, dense & rank by group) 6

 

Rank the dataframe in python pandas by Group

rank the dataframe in descending order of score by subject . so ranking is done by subject wise


# Rank by Group

df["group_rank"] = df.groupby("Subject")["Score"].rank(ascending=0,method='dense')
df

so the result will be

Rank the dataframe in python pandas – (min, max, dense & rank by group) 7

 

previous-small Rank the dataframe in python pandas – (min, max, dense & rank by group)                                                                                                          next_small Rank the dataframe in python pandas – (min, max, dense & rank by group)

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts