Python Pandas Series.cumprod() Method
In this tutorial, we will discuss and learn the Python pandas Series.cumprod()
method. This method returns a Series of the same size which consists of a cumulative product where the first element remains the same.
The below shows the syntax of the Series.cumprod()
method.
Syntax
Series.cumprod(axis=None, skipna=True, *args, **kwargs)
Parameters
axis: It represents axis 0 or ‘index’ for row axis, and 1 or ‘columns’ for column axis, and the default is 0.
skipna: It represents bool(True or False), and the default is True. It excludes all null values. If an entire row/column is NA, the result will be NA.
*args, **kwargs: It is the additional keywords that have no effect but might be accepted for compatibility with NumPy.
Example: Finding cumulative product using the Series.cumprod()
method
Here, in this example, we defined Series, and using the Series.cumprod()
method, we are finding the cumulative product of the Series. See the below example. The Series.cumprod()
method returns a Series where the first element is unchanged and the second element is the product of the first element and second element and the third element is the product of the second element and third element and so on.
#importing pandas as pd
import pandas as pd
series_1 = pd.Series([1,2,3,4])
print("------Series--------")
print(series_1)
print("----The cumulative product values are---")
print(series_1.cumprod())
------Series--------
0 1
1 2
2 3
3 4
dtype: int64
----The cumulative product values are---
0 1
1 2
2 6
3 24
dtype: int64
Example: Finding cumulative product using the Series.cumprod()
method
This example is similar to the previous one. Here, in this example, we defined Series with null values. By default, the Series.cumprod()
method skips the null values or missing values while calculating the cumulative product of the Series. See the below example.
For the third element the Series.cumprod()
method calculates cumulative product by considering the first element and third element instead of the second element.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,np.nan,6,2])
print("------Series--------")
print(series_1)
print("The cumulative product values are")
print(series_1.cumprod())
------Series--------
0 1.0
1 NaN
2 6.0
3 2.0
dtype: float64
The cumulative product values are
0 1.0
1 NaN
2 6.0
3 12.0
dtype: float64
Example: Finding cumulative product using the Series.cumprod()
method
In the last example, we have seen the Series.cumprod()
method does not consider null values while calculating cumulative product. If we want to consider those null values, we can set parameter skipna=False
. See the below example.
The cumulative product between the null value and any integer will be null values.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,np.nan,7,6])
print("------Series--------")
print(series_1)
print("The cumulative product values are")
print(series_1.cumprod(skipna=False))
------Series--------
0 1.0
1 NaN
2 7.0
3 6.0
dtype: float64
The cumulative product values are
0 1.0
1 NaN
2 NaN
3 NaN
dtype: float64
Example: Finding cumulative product using the Series.cumprod()
method
If the Series contains all elements as null values the Series.cumprod()
method returns Series with all null values. See the below example.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([np.nan,np.nan,np.nan])
print("------Series--------")
print(series_1)
print("The cumulative product values are")
print(series_1.cumprod(skipna=False))
------Series--------
0 NaN
1 NaN
2 NaN
dtype: float64
The cumulative product values are
0 NaN
1 NaN
2 NaN
dtype: float64
Conclusion
In this tutorial, we learned the Python pandas Series.cumprod()
method. We learned the syntax, parameters and by solving examples we understood the Series.cumprod()
method.