Pandas DataFrame.cumprod: Cumulative Product of DataFrame Elements


Pandas DataFrame.cumprod

The DataFrame.cumprod method in pandas is used to compute the cumulative product of DataFrame elements along a specified axis, excluding NaN values by default. This is useful for calculating running products in a DataFrame.


Syntax

The syntax for DataFrame.cumprod is:

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

Here, DataFrame refers to the pandas DataFrame on which the cumulative product operation is performed.


Parameters

ParameterDescription
axisThe axis along which the cumulative product is calculated. Use 0 or 'index' for rows, and 1 or 'columns' for columns. Defaults to None.
skipnaExcludes NaN values when performing the operation. If False, any NaN value propagates in the result. Defaults to True.
*argsAdditional positional arguments for compatibility (not used).
**kwargsAdditional keyword arguments for compatibility (not used).

Returns

A DataFrame containing the cumulative product of elements along the specified axis. If the input DataFrame is empty, the result is an empty DataFrame.


Examples

Computing the Cumulative Product Along Rows in a DataFrame

This example demonstrates how to compute the cumulative product along rows in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8]
})

# Compute cumulative product along rows
result = df.cumprod(axis=0)
print(result)

Output

     A    B
0    1    5
1    2   30
2    6  210
3   24 1680

Computing the Cumulative Product Along Columns in a DataFrame

This example demonstrates how to compute the cumulative product along columns in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Compute cumulative product along columns
result = df.cumprod(axis=1)
print(result)

Output

    A   B
0   1   4
1   2  10
2   3  18

Handling Missing Values in a DataFrame

This example demonstrates how DataFrame.cumprod excludes or propagates NaN values depending on the skipna parameter.

Python Program

import pandas as pd

# Create a DataFrame with missing values
df = pd.DataFrame({
    'A': [1, None, 3, 4],
    'B': [5, 6, None, 8]
})

# Compute cumulative product excluding NaN
result_skipna = df.cumprod(skipna=True)
print("Excluding NaN:")
print(result_skipna)

# Compute cumulative product propagating NaN
result_propagate = df.cumprod(skipna=False)
print("\nPropagating NaN:")
print(result_propagate)

Output

Excluding NaN:
     A       B
0  1.0   5.0
1  1.0  30.0
2  3.0  30.0
3 12.0 240.0

Propagating NaN:
     A    B
0  1.0  5.0
1  NaN  NaN
2  NaN  NaN
3  NaN  NaN

Summary

In this tutorial, we explored the DataFrame.cumprod method in pandas. Key takeaways include:

  • Using cumprod to compute cumulative products along rows or columns.
  • Handling missing values with the skipna parameter.
  • Specifying the axis for cumulative product computation.

Python Libraries