Cumulative product of column in pandas python

Cumulative product of column in pandas python is carried out using cumprod() function. Let’s see how to

  •          Get the cumulative product of column in pandas dataframe in python

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 product of a column in pandas 1

 

Cumulative product of a column in a pandas dataframe python:

Cumulative product of a column in pandas is computed using cumprod() function and stored in the new column namely cumulative_prod as shown below

df1['cumulative_prod'] = df1.Mathematics_score.cumprod()
print(df1)

so resultant dataframe will be

cumulative product of a column in pandas 2

 

p Cumulative product of column in pandas python                                                                                                           n Cumulative product of column in pandas python