Python | Pandas dataframe.cumprod ()

Python Methods and Functions

Pandas dataframe.cumprod() is used to find the cumulative product of values ​​so far visible on any axis.
Each the cell is filled with the accumulated product of the values ​​we have seen so far.

Syntax: DataFrame.cumprod (axis = None, skipna = True, * args, ** kwargs )

Parameters:
axis: {index (0), columns (1)}
skipna: Exclude NA / null values. If an entire row / column is NA, the result will be NA

Returns: cumprod: Series

Example # 1 : Use the cumprod () function to find the cumulative product of the values ​​so far visible along the index axis.

# import pandas as pd

import pandas as pd

 
# Create a data frame

df = pd.DataFrame ({ "A" : [ 5 , 3 , 6 , 4 ],

"B" : [ 11 , 2 , 4 , 3 ],

"C" : [ 4 , 3 , 8 , 5 ], 

"D " : [ 5 , 4 , 2 , 8 ]})

 
# Print the data frame
df

Output:

Now find the cumulative product of the values ​​so far visible along the index axis

# To find a cumulative product

df.cumprod (axis = 0 )

Output:

Example # 2: Use the cumprod () function to find the aggregate product tons of values ​​visible so far along the column axis.

Output:

Example # 3: Use the cumprod () function to find the cumulative product of values ​​so far visible along the index axis in a data frame with the value NaN present in the data frame.

# import pandas as pd

import pandas as pd

  
# Create data frame

df = pd.DataFrame ({ "A" : [ 5 , 3 , 6 , 4 ], 

"B" : [ 11 , 2 , 4 , 3 ],

"C" : [ 4 , 3 , 8 , 5 ], 

"D" : [ 5 , 4 , 2 , 8 ]})

  
# cumulative product along the column axis

df.cumprod (axis = 1 )

# import pandas as pd

import pandas as pd

 
# Create data frame

df = pd.DataFrame ({ "A" : [ 5  , 3 , None , 4 ],

"B" : [ None , 2 , 4 , 3 ], 

"C" : [ 4 , 3 , 8 , 5 ], 

"D" : [ 5 , 4 , 2 , None ]})

 
# To find a cumulative product

df.cumprod (axis = 0 , skipna = True )

Output:

The output is a data frame with cells containing the cumulative product of values ​​so far visible along the index axis. Any Nan value in the data frame is skipped.





Get Solution for free from DataCamp guru