我刚刚进入Pandas,尝试使用大型数据集轻松完成excel中的操作。我有一些选择的期货价格数据,我已经输入到Pandas使用:
df = pd.read_csv('TData1.csv')
这给了我一个DataFrame。数据如下:
Date,Time,Open,High,Low,Close,Volume,Tick Count
02/01/2013,05:01:00,1443.00,1443.75,1438.25,1440.25,20926,4652
02/01/2013,05:02:00,1440.25,1441.75,1440.00,1441.25,7261,1781
02/01/2013,05:03:00,1441.25,1443.25,1441.00,1443.25,5010,1014
现在,我基本上要做的是计算熊猫中的布林带。如果我在excel中,我会选择“高”,“低”,“打开”和“关闭”列的整个块,比如20行并计算标准差。
我看到pandas有rolling_std函数可以计算滚动标准偏差,但只能在一列上。如何让Python Pandas计算20个周期的“高”,“低”,“打开”和“关闭”列的滚动标准偏差?
感谢。
答案 0 :(得分:1)
您可以在整个DataFrame或子集上调用rolling_std:
>>> pd.rolling_std(df[['high','open','close','low']], 5)
像这样:
>>> df = pd.DataFrame({'high':np.random.randint(15,25,size=10), 'close':np.random.randint(15,25,size=10), 'low':np.random.randint(15,25,size=10), 'open':np.random.randint(15,25,size=10), 'a':list('abcdefghij')})
>>> df
a close high low open
0 a 16 20 18 15
1 b 21 23 22 15
2 c 20 23 21 23
3 d 19 24 24 17
4 e 23 19 20 17
5 f 15 16 19 17
6 g 19 24 23 19
7 h 21 18 17 22
8 i 22 22 17 15
9 j 19 20 17 18
>>> pd.rolling_std(df[['high','open','close','low']], 5)
high open close low
0 NaN NaN NaN NaN
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 NaN NaN NaN NaN
4 2.167948 3.286335 2.588436 2.236068
5 3.391165 3.033150 2.966479 1.923538
6 3.563706 2.607681 2.863564 2.073644
7 3.633180 2.190890 2.966479 2.880972
8 3.193744 2.645751 3.162278 2.489980
9 3.162278 2.588436 2.683282 2.607681