Quantile and Decile rank of a column in pandas python

Quantile and Decile rank of a column in pandas python is carried out using qcut() function with argument (labels=False) . Let’s see how to ·

  •  Get the Quantile rank of a column in pandas dataframe in python·
  • Get the Decile rank of a column in pandas dataframe in python

With an example for each .First let’s create a dataframe


import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
     'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa'],
   'Mathematics_score':[62,47,55,74,32,77,86]}

df1 = pd.DataFrame(df1,columns=['Name','Mathematics_score'])
print(df1)

df1 will be

Quantile and Decile rank of a column in pandas python 1

 

Quantile rank of a column in a pandas dataframe python

Quantile rank of the column (Mathematics_score) is computed using qcut() function and with argument  (labels=False) and 4 ,  and stored in a new column namely “Quantile_rank”  as shown below


df1['Quantile_rank']=pd.qcut(df1['Mathematics_score'],4,labels=False)
print(df1)

so the resultant dataframe will have quantile rank ranging from 0 to 3

Quantile and Decile rank of a column in pandas python 2

 

Decile rank of a column in a pandas dataframe python

Decile rank of the column (Mathematics_score) is computed using qcut() function and with argument  (labels=False) and 10 ,  and stored in a new column namely “Decile_rank”  as shown below


df1['Decile_rank']=pd.qcut(df1['Mathematics_score'],10,labels=False)
print(df1)

Quantile and Decile rank of a column in pandas python 3

so the resultant dataframe will have decile rank ranging from 0 to 9

Quantile and Decile rank of a column in pandas python                                                                                                                Quantile and Decile rank of a column in pandas python

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