How to Use cumprod in pandas for Cumulative Product Calculation
In pandas, use
cumprod() to calculate the cumulative product of values in a Series or DataFrame along a specified axis. It multiplies values sequentially, returning a new object with the cumulative product at each step.Syntax
The cumprod() function syntax is:
Series.cumprod(axis=None, skipna=True, *args, **kwargs)DataFrame.cumprod(axis=0, skipna=True, *args, **kwargs)
Parameters explained:
axis: For DataFrame, 0 for rows (default), 1 for columns.skipna: Whether to ignoreNaNvalues (default is True).
python
import pandas as pd # For Series series.cumprod(skipna=True) # For DataFrame df.cumprod(axis=0, skipna=True)
Example
This example shows how to calculate the cumulative product of a pandas Series and DataFrame.
python
import pandas as pd # Create a Series s = pd.Series([1, 2, 3, 4]) print('Series cumulative product:') print(s.cumprod()) # Create a DataFrame df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) print('\nDataFrame cumulative product along rows (axis=0):') print(df.cumprod()) print('\nDataFrame cumulative product along columns (axis=1):') print(df.cumprod(axis=1))
Output
Series cumulative product:
0 1
1 2
2 6
3 24
dtype: int64
DataFrame cumulative product along rows (axis=0):
A B
0 1 4
1 2 20
2 6 120
DataFrame cumulative product along columns (axis=1):
A B
0 1 4
1 2 10
2 3 18
Common Pitfalls
Common mistakes when using cumprod() include:
- Not specifying
axiscorrectly for DataFrames, leading to unexpected results. - Ignoring
NaNvalues whenskipna=False, which causes the cumulative product to becomeNaNfrom that point onward. - Using
cumprod()on non-numeric data, which will raise errors.
python
import pandas as pd # DataFrame with NaN df = pd.DataFrame({ 'A': [1, None, 3], 'B': [4, 5, None] }) # Wrong: skipna=False causes NaN to propagate print('Cumulative product with skipna=False:') print(df.cumprod(skipna=False)) # Right: skipna=True ignores NaN and continues print('\nCumulative product with skipna=True:') print(df.cumprod(skipna=True))
Output
Cumulative product with skipna=False:
A B
0 1.0 4.0
1 NaN NaN
2 NaN NaN
Cumulative product with skipna=True:
A B
0 1.0 4.0
1 1.0 20.0
2 3.0 20.0
Quick Reference
Summary tips for using cumprod():
- Use
axis=0to calculate cumulative product down rows (default). - Use
axis=1to calculate cumulative product across columns. - Set
skipna=Trueto ignore missing values. - Works on numeric data only.
Key Takeaways
Use pandas
cumprod() to get cumulative product of Series or DataFrame values.Specify
axis to control direction of cumulative product in DataFrames.Set
skipna=True to ignore NaN values and continue calculation.Avoid using
cumprod() on non-numeric data to prevent errors.Check results carefully when NaN values are present to avoid unexpected NaNs.