Pandas Series cumprod() Function:
Pandas cumprod: The cumprod() function of the Pandas Series computes the cumulative product over a DataFrame or Series axis and gives a DataFrame or Series of the same size including the cumulative product.
Syntax:
Series.cumprod(axis=None, skipna=True)
Parameters
axis: This is optional. It indicates 0 or “index”, 1 or “columns.” Cumulative products are generated for each column if the value is 0 or ‘index’. Cumulative products are generated for each row if 1 or ‘columns’ is specified. 0 is the default.
skipna: This is optional. When determining the result, specify True to exclude NA/null values. True is the default value.
Return Value:
The cumulative product of a scalar or a Series is returned by the cumprod() function.
- Python Pandas DataFrame ne() Function
- Python Pandas DataFrame ge() Function
- Python Pandas Series ge() Function
Python Pandas Series cumprod() Function
Example1
Pandas cumprod: Here, the cumprod() function returns the cumulative product of values in a given series.
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series.
- Apply cumprod() function on the given series to get the cumulative product values of all the elements of the given series and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([3, 3, 10, 5, 4, 2, 3, 6])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply cumprod() function on the given series to get the
# cumulative product values of all the elements of the given series
# and print the result.
print("The cumulative product values of all the elements of the given series:")
print(gvn_series.cumprod())
Output:
The given series is: 0 3 1 3 2 10 3 5 4 4 5 2 6 3 7 6 dtype: int64 The cumulative product values of all the elements of the given series: 0 3 1 9 2 90 3 450 4 1800 5 3600 6 10800 7 64800 dtype: int64
Example2
Here, the cumprod() function is used on a specified series/columns of a DataFrame.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe
- Get the cumulative product values of the student_marks column of the dataframe using the cumprod() function and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
"student_rollno": [1, 2, 3, 4],
"student_marks": [80, 35, 25, 90]},
index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Get the cumulative product values of the student_marks column of the
# dataframe using the cumprod() function and print the result
print("The cumulative product values of the student_marks column of the dataframe:")
print(data_frme['student_marks'].cumprod())
Output:
The given Dataframe:
student_rollno student_marks
virat 1 80
nick 2 35
jessy 3 25
sindhu 4 90
The cumulative product values of the student_marks column of the dataframe:
virat 80
nick 2800
jessy 70000
sindhu 6300000
Name: student_marks, dtype: int64