How to Calculate Rolling Correlation in Pandas

Preet Sanghavi Feb 02, 2024
How to Calculate Rolling Correlation in Pandas

This tutorial will discuss how to find rolling correlation values in Pandas.

Steps to Calculate Rolling Correlation Values in Pandas

The following are the steps to calculate rolling correlations between two columns of a Pandas dataframe.

Import Pandas

We need to start with importing the Pandas library.

import pandas as pd

Create a DataFrame

Let us now create a sample Pandas dataframe with two columns between which we will calculate the rolling correlation.

data = {"Data1": [1, 4, 7, 10], "Data2": [2, 5, 8, 11]}
df = pd.DataFrame(data)

We have created a dictionary named data with two columns, Data1 and Data2, and passed this dictionary to the pd.DataFrame() function to create a Pandas dataframe as shown below.

print(df)

Output:

   Data1  Data2
0      1      2
1      4      5
2      7      8
3     10     11

Calculate Rolling Correlation

We will roll our first column using the rolling() function in Pandas and then calculate the correlation of the rolled column with the other column in our data frame using the corr() function.

rc = df["Data1"].rolling(2).corr(df["Data2"])

We pass the window length of two observations to roll our first column by 2 and correlate it to the second column. We store the value of correlations in a new variable.

Let us now print the new variable to see the value of rolling correlations between the two columns.

print(rc)

Output:

0    NaN
1    1.0
2    1.0
3    1.0

The above output shows the rolling correlation values between our two columns in the dataframe. Thus, we can successfully determine the required rolling correlations values between two dataframe columns in Pandas using the above technique.

Preet Sanghavi avatar Preet Sanghavi avatar

Preet writes his thoughts about programming in a simplified manner to help others learn better. With thorough research, his articles offer descriptive and easy to understand solutions.

LinkedIn GitHub

Related Article - Pandas DataFrame