w3resource

Pandas Series: cumprod() function

Cumulative product of a Pandas series

The cumprod() function is used to get cumulative product over a DataFrame or Series axis.

Returns a DataFrame or Series of the same size containing the cumulative product.

Syntax:

Series.cumprod(self, axis=None, skipna=True, *args, **kwargs)
Pandas Series cumprod image

Parameters:

Name Description Type/Default Value Required / Optional
axis The index or the name of the axis. 0 is equivalent to None or ‘index’. {0 or ‘index’, 1 or ‘columns’}
Default Value: 0
Required
skipna Exclude NA/null values. If an entire row/column is NA, the result will be NA. boolean
Default Value: True
Required
*args, **kwargs Additional keywords have no effect but might be accepted for compatibility with NumPy. Required

Returns: scalar or Series

Example - Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, np.nan, 4, -3, 0])
s

Output:

0    2.0
1    NaN
2    4.0
3   -3.0
4    0.0
dtype: float64
Pandas Series cumprod image

Example - By default, NA values are ignored:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, np.nan, 4, -3, 0])
s.cumprod()

Output:

0     2.0
1     NaN
2     8.0
3   -24.0
4    -0.0
dtype: float64

Example - To include NA values in the operation, use skipna=False:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, np.nan, 4, -3, 0])
s.cumprod(skipna=False)

Output:

0    2.0
1    NaN
2    NaN
3    NaN
4    NaN
dtype: float64

By default, iterates over rows and finds the product in each column. This is equivalent to axis=None or axis='index'.

Previous: Cumulative minimum over a Pandas DataFrame or Series axis
Next: Cumulative sum over a Pandas DataFrame or Series axis



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-cumprod.php