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

标签 python pandas

我刚刚开始使用 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/

相关文章:

python - Anaconda 和 jupyter 笔记本 : How to switch the used virtual environment?

python - 从 URL 中获取前 n 个字节

python - 使用 Pandas 解析 json 行

python - 在 pandas 中从深格式转换为宽格式,没有内存错误

python-3.x - 在 Pandas UDF PySpark 中传递多列

Pandas - 分为 24 小时区 block ,但不是午夜到午夜

python - 如何打印 Python 列表中的每第 4 个项目?

python - 在 python 中处理长整数除法

python - 无法在 Ubuntu 上安装 ZeroRPC

Python 2.7 替换 pandas DF 中的所有值