我有一个像这样的熊猫df:
%diff
-0.087704164
0.003908466
-0.032150706
-0.035684163
0.001682029
-0.072205803
0.031636864
-0.069263158
-0.214883511
-0.109286469
0.274932615
-0.016913319
-0.075268817
0.191906977
0.043861703
-0.048598131
0.01280943
0.014509621
0.075564054
-0.024034701
0.009107468
0.023465704
我想通过'%diff列中最后20个值的标准偏差来计算252倍的平方根。
%diff std
-0.087704164
0.003908466
-0.032150706
-0.035684163
0.001682029
-0.072205803
0.031636864
-0.069263158
-0.214883511
-0.109286469
0.274932615
-0.016913319
-0.075268817
0.191906977
0.043861703
-0.048598131
0.01280943
0.014509621
0.075564054
-0.024034701 165.9%
0.009107468 163.2%
0.023465704 163.4%
我尝试的代码是:
df1['std'] = 252**(1.0/2) * df1['%diff'].std().split(20)
但是我收到了不受支持的操作数错误
答案 0 :(得分:3)
您需要rolling
的20窗口和std
像
df1['std'] = 252**(1.0/2) * df1.rolling(20)['%diff'].std()
print (df1)
%diff std
0 -0.087704 NaN
1 0.003908 NaN
2 -0.032151 NaN
3 -0.035684 NaN
4 0.001682 NaN
5 -0.072206 NaN
6 0.031637 NaN
7 -0.069263 NaN
8 -0.214884 NaN
9 -0.109286 NaN
10 0.274933 NaN
11 -0.016913 NaN
12 -0.075269 NaN
13 0.191907 NaN
14 0.043862 NaN
15 -0.048598 NaN
16 0.012809 NaN
17 0.014510 NaN
18 0.075564 NaN
19 -0.024035 1.659144
20 0.009107 1.631865
21 0.023466 1.634266