gpt4 book ai didi

python - 在Python pandas中计算4列的rolling_std来计算布林线?

转载 作者:太空宇宙 更新时间:2023-11-03 18:43:35 44 4
gpt4 key购买 nike

我刚刚开始使用 Pandas,尝试使用大型数据集轻松完成我在 excel 中会做的事情。我选择了一些 future 价格数据,并使用以下方法将其输入到 Pandas 中:

df = pd.read_csv('TData1.csv')

这给了我一个数据帧。数据格式如下:

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

现在我本质上想做的是计算 pandas 中的布林线。如果我在 Excel 中,我会选择整个“最高”、“最低”、“开盘”和“收盘”列,例如 20 行,并计算标准差。

我看到pandas有rolling_std函数,它可以计算滚动标准差,但只能计算一列。如何让 Python Pandas 计算 20 个周期的“最高价”、“最低价”、“开盘价”和“收盘价”列的滚动标准差?

谢谢。

最佳答案

您可以在整个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

关于python - 在Python pandas中计算4列的rolling_std来计算布林线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20020731/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com