Built with Tutify.io

    AIStudyLabs

    Pandas DataFrame cumprod() Method

    Pandas DataFrame cumprod() Method

    In Pandas, the cumprod() method calculates the cumulative product of a DataFrame or Series along a specified axis. By default, it performs this operation column-wise, but you can adjust the axis to perform the calculation row-wise. The result is that each value in the specified direction is the product of all preceding values, including itself.

    In this article, I will explain the Pandas DataFrame cumprod() method and using its syntax, parameters, and usage how we can generate a DataFrame that displays the cumulative product for each row.

    Key Points –

    <a id="h-pandas-dataframe-cumprod-introduction"></a>

    Pandas DataFrame cumprod() Introduction

    Following is the syntax of the Pandas DataFrame cumprod() method.

    
    # Syntax of Pandas dataframe cumprod() method
    DataFrame.cumprod(axis=None, skipna=True, *args, **kwargs)
    

    <a id="h-parameters-of-the-dataframe-cumprod"></a>

    Parameters of the DataFrame cumprod()

    Following are the parameters of the DataFrame cumprod() method.

    <a id="h-return-value"></a>

    Return Value

    It returns a DataFrame or Series with the cumulative product of the values along the specified axis.

    <a id="h-usage-of-pandas-dataframe-cumprod-method"></a>

    Usage of Pandas DataFrame cumprod() Method

    The cumprod() method in Pandas computes the cumulative product of a DataFrame or Series along a specified axis. For each element, it multiplies the current value by all the previous values in the direction of the specified axis.

    Now, let’s create a Pandas DataFrame using data from a dictionary, with columns labeled A, B, and C.

    
    import pandas as pd
    
    # Creating a sample DataFrame
    data = {
        'A': [5, 8, 2, 4],
        'B': [3, 6, 9, 2],
        'C': [4, 7, 6, 5]
    }
    
    df = pd.DataFrame(data)
    print("Original DataFrame:\n",df)
    

    Yields below output.

    <a id="h-cumulative-product-for-dataframe-columns"></a>

    Cumulative Product for DataFrame Columns

    To compute the cumulative product for each column in a DataFrame, use the cumprod() method. By default, this method calculates the cumulative product for each column across the rows (axis=0).

    
    # Computing the cumulative product along columns
    df2 = df.cumprod()
    print("Cumulative Product DataFrame:\n", df2)
    

    Here,

    <a id="h-cumulative-product-along-rows"></a>

    Cumulative Product Along Rows

    Alternatively, to calculate the cumulative product along the rows of a DataFrame, apply the cumprod() method with axis=1. This computes the cumulative product for each row across the columns.

    
    # Computing the cumulative product along rows (axis=1)
    df2 = df.cumprod(axis=1)
    print("Cumulative product along rows:\n", df2)
    
    # Output:
    # Cumulative product along rows:
    #    A   B    C
    # 0  5  15   60
    # 1  8  48  336
    # 2  2  18  108
    # 3  4   8   40
    

    Here,

    <a id="h-handling-missing-values"></a>

    Handling Missing Values

    Handling missing values when using the cumprod() method in Pandas depends on how you choose to deal with NaN (missing) values.

    <a id="h-cumulative-product-with-skipna-true"></a>

    Cumulative Product with skipna=True

    By default, when skipna=True in the cumprod() method, it ignores any NaN values and continues calculating the cumulative product with the next available numeric value. This allows the cumulative product to proceed without interruption by missing data.

    
    import pandas as pd
    import numpy as np
    
    # Creating a DataFrame with missing values
    data = {
        'A': [2, np.nan, 3, 4],
        'B': [1, 5, np.nan, 2],
        'C': [3, np.nan, 2, 4]
    }
    
    df = pd.DataFrame(data)
    
    # Cumulative product with skipna=True (default)
    cumprod_df = df.cumprod(skipna=True)
    print("\nCumulative Product with skipna=True:\n", cumprod_df)
    
    # Output:
    # Cumulative Product with skipna=True:
    #       A     B     C
    # 0   2.0   1.0   3.0
    # 1   NaN   5.0   NaN
    # 2   6.0   NaN   6.0
    # 3  24.0  10.0  24.0
    

    <a id="h-cumulative-product-with-skipna-false"></a>

    Cumulative Product with skipna=False

    When you use cumprod() with skipna=False, the method does not ignore NaN values. If a NaN is encountered, all subsequent results in that row or column will also be NaN.

    
    # Computing the cumulative product with skipna=False
    df2 = df.cumprod(skipna=False)
    print("Cumulative Product with skipna=False:\n", df2)
    
    # Output:
    # Cumulative Product with skipna=False:
    #      A    B    C
    # 0  2.0  1.0  3.0
    # 1  NaN  5.0  NaN
    # 2  NaN  NaN  NaN
    # 3  NaN  NaN  NaN
    

    <a id="h-cumulative-product-for-a-series"></a>

    Cumulative Product for a Series

    The cumulative product for a Pandas Series can be calculated using the cumprod() method, which returns a new Series where each value is the product of the current element and all preceding elements.

    
    import pandas as pd
    
    # Creating a Series
    ser = pd.Series([2, 3, 4, 5])
    
    # Calculating the cumulative product
    result = ser.cumprod()
    print("Cumulative Product of the Series:\n", result)
    
    # Output:
    # Cumulative Product of the Series:
    # 0      2
    # 1      6
    # 2     24
    # 3    120
    # dtype: int64
    

    <a id="h-faq-on-pandas-dataframe-cumprod-method"></a>

    FAQ on Pandas DataFrame cumprod() Method

    The cumprod() method in Pandas calculates the cumulative product of values in a DataFrame or Series. It is helpful in scenarios where a running product of elements needs to be determined over time or across different categories.

    To compute the cumulative product for a Series, you can use the cumprod() method directly on the Series. This method multiplies each element by all previous elements, resulting in a cumulative product at each position.

    By default (skipna=True), cumprod() skips NaN values and continues the calculation with the next valid number. If skipna=False, any encounter with NaN will return NaN for the rest of the values along that axis.

    cumprod() only works on numeric data. If the DataFrame contains mixed data types (e.g., strings), you must first convert the non-numeric values to NaN or use pd.to_numeric(errors='coerce') to convert them to numeric types.

    cumprod() works on both DataFrames and Series. For a Series, the cumulative product is computed over the values in the Series.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained the Pandas DataFrame cumprod() method by using its syntax, parameters, and usage to compute the cumulative product along a DataFrame or Series axis, resulting in a DataFrame or Series of the same dimensions with the cumulative product values.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference