## Cumulative Product Pandas

Python is a high-level programming language that provides multiple packages for analyzing data, and the “**padas**” is one of them that make data analysis easier. It includes the “**Series.cumprod()**” method, which is used for calculating the cumulative product of any list or array and returns the same length series as the provided input series.

This post will discuss:

- What are Cumulative Product Pandas in Python?
- How to Get the Cumulative Product of Series Keeping “skipna =True” in Python?
- How to Get the Cumulative Product of Series Keeping “skipna =False” in Python?
- How to Get the Cumulative Product of Series With the “axis” Parameter in Python?

## What are the Cumulative Product Pandas in Python?

In Python. the “**pandas**” library includes several functions for calculating the cumulative product of DataFrame, such as the “**cumprod()**” method. The cumulative product contains current values and all the previous values in the form of an array. The “**cumprod()**” method is the most commonly used method for finding the cumulative sum in Python. To do so, first, the series of elements are provided to the “**cumprod()**” function and obtained the same size of Dataframe having the cumulative product.

**Syntax**

Now, check the syntax of the “**cumprod()**” method:

According to the above-stated block of code, the specified method takes two arguments, such as “**axis**” and “**skipna**”. The “**axis**” takes two values either (0 or 1), or (index or column), and 0. Both indicate the tuple operation. On the other hand, the “**skipna**” parameter takes a boolean value that is utilized for skipping over the NULL values in a DataFrame.

## How to Get the Cumulative Product of Series Keeping “skipna =True” in Python?

To calculate the cumulative product of the series containing the NULL values keeping the “**skipna= True**”. First, generate a small DataFrame of numbers that also includes NULL values and passes them to the list variable “**vList**”. After that, called the “**series()**” method for generating the series from the previously created list. In the next line of code, we have invoked the “**cumprod()**” method and passed the “**skipna**” as “**True**” and saved it in the variable known as “**cumprod**”. Then, displayed the cumulative product list:

import numpy as np

vList = [6, 3, 8, np.nan, 1, 3]

listSeries = pd.Series(vList)

cumprod = listSeries.cumprod(skipna = True)

cumprod

As you can see, the first element of the cumulative product list remains the same. However, the next value is the product of the 1st and 2nd values, and so on. The cumulative product list also contains the “**NaN**” value because the provided series of elements have the NULL value:

## How to Get the Cumulative Product of Series Keeping “skipna =False” in Python?

If the “**skipna**” parameter takes a “**False**” value, it means if the NULL value exists in the provided list, it will not be ignored and used for comparing every time its occurrence. Let’s check out the provided example for a better understanding. Here, we have specified the “**skipna**” value as “**False**” and then, displayed the cumulative product list:

cumprod

It can be observed that the NaN value is not ignored while comparing values for getting the cumulative product after the occurrence of NaN value:

## How to Get the Cumulative Product of Series With the “axis” Parameter in Python?

To calculate the cumulative product of an array of just one “**axis**” at once, we have imported the “**numpy**” module “**np**”. Then, defined the array and passed to the “**cumprod()**” function with the “**axis**” with value “**1**”. The “**cumprod()**” function will calculate the cumulative product of the first column, return the results and repeat until all column values are finished. After that, invoked the “**print()**” function to obtain the cumulative product of the specified array:

arr1 = np.array([[7, 3, 2], [7, 4,1]])

axis1 = np.cumprod(arr1, axis = 1)

print("Cumulative Product = ", axis1)

**Output**

That’s it! You have learned the process of obtaining the cumulative product of a list and an array in Python.

## Conclusion

In Python, the cumulative product includes the current as well as all the previous values in the form of an array. To do so, the “**cumprod()**” method is the most widely utilized in Python. This post demonstrated the easiest ways to calculate the cumulative product of a list and an array in Python.

Source: linuxhint.com