Python | Pandas series.cumprod() to find Cumulative product of a Series
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas Series.cumprod() is used to find Cumulative product of a series. In cumulative product, the length of returned series is same as input series and every element is equal to the product of current and all previous values.
Syntax: Series.cumprod(axis=None, skipna=True)
Parameters:
axis: 0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation
skipna: Skips NaN addition for elements after the very next one if True.Return type: Series
Example #1:
In this example, a series is created from a Python list. The list also contains a Null value and the skipna parameter is kept default, that is True.
# importing pandas module import pandas as pd # importing numpy module import numpy as np # making list of values values = [2, 10, np.nan, 4, 3, 0, 1] # making series from list series = pd.Series(values) # calling method cumprod = series.cumprod() # display cumprod |
Output:
0 2.0 1 20.0 2 NaN 3 80.0 4 240.0 5 0.0 6 0.0 dtype: float64
Explanation: Cumprod is multiplication of current and all previous values.hence, the first element is always equal to first of caller series.
2 20 (2 x 10) NaN (20 x NaN = NaN, Anything multiplied with NaN returns NaN) 80 (20 x 4) 240 (80 x 3) 0 (240 x 0) 0 (0 x 1)
Example #2: Keeping skipna=False
In this example, a series is created just like in the above example. But the skipna parameter is kept False. Hence NULL values won’t be ignored and it would be compared every time on it’s occurrence.
# importing pandas module import pandas as pd # importing numpy module import numpy as np # making list of values values = [9, 4, 33, np.nan, 0, 1, 76, 5] # making series from list series = pd.Series(values) # calling method cumprod = series.cumprod(skipna = False) # display cumprod |
Output:
0 9.0 1 36.0 2 1188.0 3 NaN 4 NaN 5 NaN 6 NaN 7 NaN dtype: float64
Explanation: Just like in the above example, product of current and all previous values was returned at every position. Since NaN Multiplied with anything is also NaN, and skipna parameter was kept False, Hence all values after occurrence of NaN are also NaN.
Recommended Posts:
- Python | Pandas Series.cummin() to find cumulative minimum of a series
- Python | Pandas series.cummax() to find Cumulative maximum of a series
- Python | Pandas Series.cumsum() to find cumulative sum of a Series
- Python | Pandas Series.product()
- Python | Pandas Series.str.find()
- Python | Pandas Series.cov() to find Covariance
- Python program to find Cumulative sum of a list
- Python | Pandas Series.astype() to convert Data type of series
- Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series
- Python | Pandas Series.nonzero() to get Index of all non zero values in a series
- Python | Pandas dataframe.product()
- Python | Pandas Series.loc
- Python | Pandas Series.str.contains()
- Python | Pandas Series.min()
- Python | Pandas Series.ix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



