Cumulative percentage of a column in pandas python

Cumulative percentage of a column in pandas python is carried out using sum() and cumsum() function in roundabout way. Let’s see how to

  • Get the cumulative percentage of a column in pandas dataframe in python With an example.

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

Cumulative percentage of a column in pandas python - image Cumulative-percentage-of-a-column-in-pandas-python-1 on http://www.datasciencemadesimple.com

 

 Cumulative percentage of a column in a pandas dataframe python 

Cumulative percentage of a column in pandas dataframe is computed using cumsum() and sum() function and stored in a new column namely cumulative_percentage as shown below


df1['cumulative_percentage'] = 100*df1.Mathematics_score.cumsum()/df1.Mathematics_score.sum()
print(df1)

so resultant dataframe will be

Cumulative percentage of a column in pandas python - image Cumulative-percentage-of-a-column-in-pandas-python-2 on http://www.datasciencemadesimple.com

 

Cumulative percentage of a column in pandas python prev                                                                                                                Cumulative percentage of a column in pandas python