Articles

cumprod pandas example | pandas cumprod skipna | pandas cumprod across columns

cumprod pandas example | pandas cumprod skipna | pandas cumprod across columns


In this pandas tutorial, we will discuss about:

  • pandas cumprod method,
  • cumprod pandas example

Before going ahead with understanding pandas cumprod method and cumprod pandas example lets have little knowledge about pandas dataframe.

 

DataFrame in pandas is a two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe, the data can be a dictionary that stores list of values with specified key.

 

Example: Create dataframe

In this example, we will create a dataframe with 4 rows and 3 columns with building data and assign indices through index parameter.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: Dataframe is created

       length  breadth  area
one       5.6     12.9    20
two       7.8      4.5    56
three     4.5     21.5    43
four      5.3      6.0    45

Lets use this dataframe to understand cumprod pandas example.


Pandas cumprod

pandas cumprod() will return the cumulative product of values for the given dataframe.

Syntax:

dataframe.cumprod(axis,skipna)

This will return the entire dataframe

Parameters:

  1. axis=0 specifies row and axis= 1 specifies column to get cumulative product along row/column
  2. pandas cumprod skipna will take boolean values - True or False. If False,It will consider NaN values and If True,It will not  consider NaN values in cumulative product operation.

If we want to get the cumulative product of values in a column for the given dataframe, we have to specify the column.

Syntax:

dataframe['column'].cumprod()

where, dataframe is the input dataframe and column is the column name.

Pandas cumprod across columns will return the specified column cumulative product in the given dataframe.

Lets now see few cumprod pandas example.

 

cumprod pandas examplepandas cumprod across columns

In this cumprod pandas example, we will get the cumulative product for the entire dataframe and in a specific column.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# get the cumulative product 
print(data.cumprod())

print()

# get the cumulative product from length column
print(data['length'].cumprod())

print()

# get the cumulative product from area column
print(data['area'].cumprod())

Outputcumprod pandas example result is shown below

         length   breadth     area
one       5.600    12.900       20
two      43.680    58.050     1120
three   196.560  1248.075    48160
four   1041.768  7488.450  2167200

one         5.600
two        43.680
three     196.560
four     1041.768
Name: length, dtype: float64

one           20
two         1120
three      48160
four     2167200
Name: area, dtype: int64

Lets now see pandas cumprod skipna example.


cumprod pandas examplepandas cumprod skipna

In this pandas cumprod skipna example. we will be dealing with skipna parameter.

import pandas as pd
import numpy as np

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[np.nan,7.8,4.5,np.nan],

                   "breadth":[12.9,4.5,21.5,np.nan],

                    "area":[2,np.nan,56,43]

                   },index=['one','two','three','four'])

# get the cumulative product with out nan values
print(data.cumprod(skipna=True))

print()

# get the cumulative product  by considering nan values
print(data.cumprod(skipna=False))

Outputpandas cumprod skipna example result is shown below

       length   breadth    area
one       NaN    12.900     2.0
two       7.8    58.050     NaN
three    35.1  1248.075   112.0
four      NaN       NaN  4816.0

       length   breadth  area
one       NaN    12.900   2.0
two       NaN    58.050   NaN
three     NaN  1248.075   NaN
four      NaN       NaN   NaN

Thus we have learned about cumprod pandas example for both pandas cumprod skipna and pandas cumprod across columns.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 07,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!