- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.cumprod#
- DataFrame.cumprod(axis=None,skipna=True,*args,**kwargs)[source]#
Return cumulative product over a DataFrame or Series axis.
Returns a DataFrame or Series of the same size containing the cumulativeproduct.
- 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’.ForSeries this parameter is unused and defaults to 0.
- skipnabool, default True
Exclude NA/null values. If an entire row/column is NA, the resultwill be NA.
- *args, **kwargs
Additional keywords have no effect but might be accepted forcompatibility with NumPy.
- Returns:
- Series or DataFrame
Return cumulative product of Series or DataFrame.
See also
core.window.expanding.Expanding.prodSimilar functionality but ignores
NaNvalues.DataFrame.prodReturn the product over DataFrame axis.
DataFrame.cummaxReturn cumulative maximum over DataFrame axis.
DataFrame.cumminReturn cumulative minimum over DataFrame axis.
DataFrame.cumsumReturn cumulative sum over DataFrame axis.
DataFrame.cumprodReturn cumulative product over DataFrame axis.
Examples
Series
>>>s=pd.Series([2,np.nan,5,-1,0])>>>s0 2.01 NaN2 5.03 -1.04 0.0dtype: float64
By default, NA values are ignored.
>>>s.cumprod()0 2.01 NaN2 10.03 -10.04 -0.0dtype: float64
To include NA values in the operation, use
skipna=False>>>s.cumprod(skipna=False)0 2.01 NaN2 NaN3 NaN4 NaNdtype: float64
DataFrame
>>>df=pd.DataFrame([[2.0,1.0],...[3.0,np.nan],...[1.0,0.0]],...columns=list('AB'))>>>df A B0 2.0 1.01 3.0 NaN2 1.0 0.0
By default, iterates over rows and finds the productin each column. This is equivalent to
axis=Noneoraxis='index'.>>>df.cumprod() A B0 2.0 1.01 6.0 NaN2 6.0 0.0
To iterate over columns and find the product in each row,use
axis=1>>>df.cumprod(axis=1) A B0 2.0 2.01 3.0 NaN2 1.0 0.0