Row wise percentage in pandas dataframe

In This Section we will be focusing on different methods to perform row wise percentage calculation of the pandas dataframe. i.e., Row wise percentage of all the numeric column in pandas python. There are multiple ways to do it, we have used functions like iloc(), sum() along with mul() function to achieve the same. let’s look at each of these cases in pandas with an example for each.

  • Row wise percentage calculation of all columns in pandas
  • Row wise percentage in pandas with default decimal places summing to 1
  • Row wise percentage in pandas with 2 decimal places

 

Create Dataframe:

 

## create dataframe

import pandas as pd
import numpy as np

d = { 'Name':['Alisa','Bobby','Cathrine','Jodha','Raghu','Ram'],
'Maths':[76,73,83,93,89,94],
'Science':[85,41,55,75,81,97],
'Geography':[78,65,55,88,87,98]}

df = pd.DataFrame(d,columns=['Name','Maths','Science','Geography'])
df

resultant dataframe is

Row-wise-percentage-calculation-in-pandas-1

 

 

Row wise percentage calculation of all columns in pandas with default decimal places summing to 1

We slice the correct columns with iloc. Use apply with axis=1 to apply each calculation row wise.  In Our below Example except first column all the other columns are converted to percentage values

 
## Row wise percentage in pandas with default decimal places summing to 1

df.iloc[:, 1:] = df.iloc[:, 1:].apply(lambda x: x.div(x.sum()), axis=1).astype(float)
df

so the resultant dataframe has the percentage distribution with all values summing upto 1.

Row-wise-percentage-calculation-in-pandas-2

 

 

 

Row wise percentage in pandas dataframe with 2 decimal places:

We slice the correct columns with iloc Use apply with axis=1 to apply each calculation row wise. We use div(), sum() and mul() to divide each value to the rows sum and multiply it by 100 to get the percentages in whole numbers not decimals.

 
## Row wise percentage in pandas with 2 decimal places

df.iloc[:, 1:] = df.iloc[:, 1:].apply(lambda x: x.div(x.sum()).mul(100), axis=1).astype(float).round(2)
df

so the resultant dataframe has the percentage distribution with all values summing upto 100 percent.

Row-wise-percentage-calculation-in-pandas-3

 

 

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.