Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将Excel关联窗口与关联窗口对齐_Python_Excel_Pandas_Time Series_Rolling Computation - Fatal编程技术网

Python 将Excel关联窗口与关联窗口对齐

Python 将Excel关联窗口与关联窗口对齐,python,excel,pandas,time-series,rolling-computation,Python,Excel,Pandas,Time Series,Rolling Computation,我在EXCEL中使用Var_1和Var_2的3个时段窗口创建了一个滚动关联。EXCEL代码为: =CORREL(B2:B4,C2:C4) 我试图在Python中创建相同的结果。然而,当我执行代码时,我的结果在Python中下移1行。我还得到了一个1.0作为第一行值,我不明白。 该工作表从Excel读入Python并保存为数据框。 这是图片。 我创建的python代码是为了重新创建这种滚动关联 df2 = pd.DataFrame((df.iloc[::1,1]).rolling(window

我在EXCEL中使用Var_1和Var_2的3个时段窗口创建了一个滚动关联。EXCEL代码为:

=CORREL(B2:B4,C2:C4)
我试图在Python中创建相同的结果。然而,当我执行代码时,我的结果在Python中下移1行。我还得到了一个1.0作为第一行值,我不明白。 该工作表从Excel读入Python并保存为数据框。 这是图片。 我创建的python代码是为了重新创建这种滚动关联

df2 = pd.DataFrame((df.iloc[::1,1]).rolling(window = 3,min_periods = 1,center = True).corr((df.iloc[::1,2])))

您可能可以将统计模块与列表理解一起使用:

df['r_value'] = [scipy.stats.linregress(df['Var_1'].loc[i:i+2], df['Var_2'].loc[i:i+2])[2] for i in range(len(df))]

    Var_1   Var_2   r_value
0   5        -55    -0.525909
1   41       -44    -0.455413
2   85       -65    0.032059
3   55       -77    0.896258
4   65       -25    0.388874
5   47       -77    0.474843
6   25       -48    1.000000
7   63       -12    0.000000