Signup/Sign In

Pandas DataFrame cumprod() Method

In this tutorial, we will learn the Python pandas DataFrame.cumprod() method. It gives a cumulative product over a DataFrame or Series axis. It returns a DataFrame or Series of the same size containing the cumulative product.

The below shows the syntax of the Python pandas DataFrame.cumprod() method.

Syntax

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

Parameters:

axis: {0 or ‘index’, 1 or ‘columns’}, default 0. The index or the name of the axis. 0 is equivalent to None or ‘index’.

skipna: bool, default True. Exclude NA/null values. If an entire row/column is NA, the result will be NA.

*args, **kwargs: Additional keywords have no effect but might be accepted for compatibility with NumPy.

Example 1: Finding the cumulative product of the DataFrame

The below example shows how to find the cumulative product of the DataFrame over the index axis using the DataFrame.cumprod() method.

import pandas as pd  
# Creating the dataframe 
df = pd.DataFrame({"A":[1, 2, 3, 4], "B":[5, 6, 7, 8]})
print(df)
print("-----------Finding cumulative product-------")
print(df.cumprod(axis = 0))

Once we run the program we will get the following output.


A B
0 1 5
1 2 6
2 3 7
3 4 8
-----------Finding cumulative product-------
A B
0 1 5
1 2 30
2 6 210
3 24 1680

Example 2: Finding the cumulative product of the DataFrame

The below example shows how to find the cumulative product of the DataFrame over the column axis using the DataFrame.cumprod() method.

import pandas as pd  
# Creating the dataframe 
df = pd.DataFrame({"A":[1, 2, 3, 4], "B":[5, 6, 7, 8]})
print(df)
print("-----------Finding cumulative product-------")
print(df.cumprod(axis = 1))

Once we run the program we will get the following output.


A B
0 1 5
1 2 6
2 3 7
3 4 8
-----------Finding cumulative product-------
A B
0 1 5
1 2 12
2 3 21
3 4 32

Example 3: Finding the cumulative product of the DataFrame

The below example shows how to find the cumulative product of the DataFrame with null values over the index axis using the DataFrame.cumprod() method.

import pandas as pd  
# Creating the dataframe 
df = pd.DataFrame({"A":[1, 2, 3, 4], "B":[5, 6, None, 8]}) 
print(df)
print("-----------Finding cumulative product-------")
print(df.cumprod(skipna=False))

Once we run the program we will get the following output.


A B
0 1 5.0
1 2 6.0
2 3 NaN
3 4 8.0
-----------Finding cumulative product-------
A B
0 1 5.0
1 2 30.0
2 6 NaN
3 24 NaN

Conclusion

In this tutorial, we learned the Python pandas DataFrame.cumprod() method. We learned the syntax, parameters and by solving examples we understood the DataFrame.cumprod() method.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.

Want to learn coding and don't know where to start?

Try out our Interactive Courses for Free 🥳 😯 🤩
learn to code footer Ad