How to Calculate Rolling Correlation in Python? Last Updated : 29 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Correlation generally determines the relationship between two variables. The rolling correlation measure the correlation between two-time series data on a rolling window Rolling correlation can be applied to a specific window width to determine short-term correlations. Calculating Rolling Correlation in Python Let's use sales data of two products A and B in the last 60 months to calculate the rolling correlation. Pandas package provides a function called rolling.corr() to calculate the rolling correlation. Syntax: data1.rolling(width).corr(data2) Where, data1, data2 - data/column of interest (type series)width - Rolling window width (int) Note: The width of the rolling window should be 3 or greater in order to calculate correlations. Data Used: Python3 # import pandas module import pandas as pd # read the data data = pd.read_csv('product_sales.csv') # display top 10 rows print(data.head(10)) # display column names print(data.columns) Output: Example 2: Here, we used the window width of 6, which shows the successive 6 months rolling correlation. We could see a significant correlation between two products sales any sudden dip or rise in correlation signals an unusual event, that caused the dip. Python3 data['Product A'].rolling(6).corr(data['Product B']) # formatting the output k = 1 for i, j in enumerate(data['Product A'].rolling(6).corr(data['Product B'])): if (i >= 5 and i < 12): print(f'The correlation in sales during months\ {k} through {i+1} is {j}') i = 0 k += 1 Output: Now's let us try the same for 3-month correlation as shown below, Example 3: Python3 data['Product A'].rolling(3).corr(data['Product B']) # formatting the output k = 1 for i, j in enumerate(data['Product A'].rolling(3).corr(data['Product B'])): if (i >= 3 and i < 12): print( f'The correlation in sales during months {k} \ through {i+1} is {j}') i = 0 k += 1 Output: Comment More infoAdvertise with us Next Article How to Calculate Point-Biserial Correlation in R? J jssuriyakumar Follow Improve Article Tags : Python Python-pandas Practice Tags : python Similar Reads How to Calculate Rolling Correlation in R? In this article, we will discuss Rolling Correlation in R Programming Language. Correlation is used to get the relationship between two variables. It will result in 1 if the correlation is positive.It will result in -1 if the correlation is negative.it will result in 0 if there is no correlation. Ro 2 min read How to Calculate Polychoric Correlation in R? In this article, we will discuss how to calculate polychoric correlation in R Programming Language. Calculate Polychoric Correlation in R Correlation measures the relationship between two variables. we can say the correlation is positive if the value is 1, the correlation is negative if the value is 2 min read How to Calculate Partial Correlation in R? In this article, we will discuss how to calculate Partial Correlation in the R Programming Language. Partial Correlation helps measure the degree of association between two random variables when there is the effect of other variables that control them. in partial correlation in machine learning It g 3 min read How to Calculate Point-Biserial Correlation in R? In this article, we will discuss how to calculate Point Biserial correlation in R Programming Language. Correlation measures the relationship between two variables. we can say the correlation is positive if the value is 1, the correlation is negative if the value is -1, else 0. Point biserial correl 2 min read How to Calculate Cross Correlation in R? In this article we will discuss how to calculate cross correlation in R programming language. Correlation is used to get the relation between two or more variables. The result is 0, if there is no correlation between two variablesThe result is 1, if there is positive correlation between two variable 1 min read How to Calculate Point-Biserial Correlation in Excel? The Point-Biserial Correlation Coefficient is a correlation metric that measures the degree of relationship between a continuous and a binary variable. The connection between a binary variable, x, and a continuous variable, y, is measured using point-biserial correlation. Binary variables are widely 3 min read Like