Pandas rolling apply multiple columns

Query: pandas rolling apply multiple columns

In pandas, the rolling apply function is used to apply custom functions on a rolling window. It is often used to calculate rolling statistics or perform rolling computations on multiple columns of a DataFrame.
Let’s consider an example to illustrate how to use the rolling apply function on multiple columns.

        import pandas as pd

        # Create a sample DataFrame
        data = {'A': [1, 2, 3, 4, 5],
                'B': [6, 7, 8, 9, 10],
                'C': [11, 12, 13, 14, 15]}

        df = pd.DataFrame(data)

        # Define a custom function to calculate the sum of two columns
        def sum_two_columns(x):
            return x['A'] + x['B']

        # Apply the custom function on a rolling window of size 3
        rolling_sum = df.rolling(window=3).apply(sum_two_columns)

        print(rolling_sum)
    

The above code creates a sample DataFrame with three columns (A, B, and C). It then defines a custom function sum_two_columns() that takes a DataFrame as an input and returns the sum of columns ‘A’ and ‘B’. The df.rolling(window=3).apply(sum_two_columns) line applies the custom function on a rolling window of size 3 to calculate the rolling sum of columns ‘A’ and ‘B’.

The output of the code will be:

           A     B     C
        0  NaN   NaN   11.0
        1  NaN   NaN   15.0
        2  12.0  21.0  19.0
        3  15.0  24.0  23.0
        4  18.0  27.0  27.0
        

As you can see, the rolling sum is calculated for each column ‘A’ and ‘B’ separately. The first two rows are NaN because the rolling window size is set to 3, so the sum cannot be calculated for the first two rows.

You can modify the custom function to perform different computations on multiple columns based on your requirements. The rolling apply function provides flexibility in applying custom functions over rolling windows in pandas.

Leave a comment