Quantile Rank
Algorithm :
- Import
pandas
andnumpy
modules. - Create a dataframe.
- Use
pandas.qcut()
function, theScore
column is passed, on which the quantile discretization is calculated. Andq
is set to 4 so the values are assigned from 0-3 - Print the dataframe with the quantile rank.
# importing the modules import pandas as pd import numpy as np # creating a DataFrame df = { 'Name' : [ 'Amit' , 'Darren' , 'Cody' , 'Drew' , 'Ravi' , 'Donald' , 'Amy' ], 'Score' : [ 50 , 71 , 87 , 95 , 63 , 32 , 80 ]} df = pd.DataFrame(df, columns = [ 'Name' , 'Score' ]) # adding Quantile_rank column to the DataFrame df[ 'Quantile_rank' ] = pd.qcut(df[ 'Score' ], 4 , labels = False ) # printing the DataFrame print (df) |
Output :
Decile Rank
Algorithm :
- Import
pandas
andnumpy
modules. - Create a dataframe.
- Use
pandas.qcut()
function, theScore
column is passed, on which the quantile discretization is calculated. Andq
is set to 10 so the values are assigned from 0-9 - Print the dataframe with the decile rank.
# importing the modules import pandas as pd import numpy as np # creating a DataFrame df = { 'Name' : [ 'Amit' , 'Darren' , 'Cody' , 'Drew' , 'Ravi' , 'Donald' , 'Amy' ], 'Score' : [ 50 , 71 , 87 , 95 , 63 , 32 , 80 ]} df = pd.DataFrame(df, columns = [ 'Name' , 'Score' ]) # adding Decile_rank column to the DataFrame df[ 'Decile_rank' ] = pd.qcut(df[ 'Score' ], 10 , labels = False ) # printing the DataFrame print (df) |
Output :
Quantile and Decile rank of a column in Pandas-Python
Let’s see how to find the Quantile and Decile ranks of a column in Pandas. We will be using the qcut()
function of the pandas
module.