Pandas DataFrame | cumprod method
Start your free 7-days trial now!
Pandas DataFrame.cumprod(~)
method computes the cumulative product along the row or column of the source DataFrame.
Parameters
1. axis
link | int
or string
| optional
Whether to compute the cumulative product along the row or the column:
Axis | Description |
---|---|
| Compute the cumulative product of each column. |
| Compute the cumulative product of each row. |
By default, axis=0
.
2. skipna
link | boolean
| optional
Whether or not to ignore NaN
. By default, skipna=True
.
Return Value
A DataFrame holding the cumulative product of the row or column values.
Examples
Consider the following DataFrame:
df
A B0 3 51 4 6
Cumulative product of each column
To compute the cumulative product for each column:
df.cumprod()
A B0 3 51 12 30
Cumulative product of each row
To compute the cumulative product for each row:
df.cumprod(axis=1)
A B0 3 151 4 24
Dealing with missing values
Consider the following DataFrame with a missing value:
df
A0 3.01 NaN2 5.0
By default, skipna=True
, which means that missing values are ignored:
df.cumprod() # skipna=True
A0 3.01 NaN2 15.0
To take into account missing values:
df.cumprod(skipna=False)
A0 3.01 NaN2 NaN
Here, notice how we end up with a NaN
after the first NaN
.