python - Dataframe 简单移动平均线 (SMA) 计算

标签 python pandas dataframe

是否有任何简单的工具/库可以帮助我轻松计算数据帧的简单移动平均线 SMA(N)?

                             GLD       SMA(5)
Date                                
2005-01-03 00:00:00+00:00  43.020000  Nan
2005-01-04 00:00:00+00:00  42.740002  Nan
2005-01-05 00:00:00+00:00  42.669998  Nan
2005-01-06 00:00:00+00:00  42.150002  Nan
2005-01-07 00:00:00+00:00  41.840000  ..
2005-01-10 00:00:00+00:00  41.950001  ..
2005-01-11 00:00:00+00:00  42.209999  ..
2005-01-12 00:00:00+00:00  42.599998  ..
2005-01-13 00:00:00+00:00  42.599998  ..
2005-01-14 00:00:00+00:00  42.320000  ..

最佳答案

df['SMA(5)'] = df.GLD.rolling(5).mean()
df

enter image description here

关于python - Dataframe 简单移动平均线 (SMA) 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39138299/

相关文章:

python - 启动 celery worker throws "no attribute ' worker_state_db'"

python - 如果列存在于 df 中,如何根据条件删除 Pandas 中的列数据框 (df)?

r - S4 类列表到数据框

regex - 根据其他列向 Panda 数据框添加新列

python - 无法在 Python 3.5 上安装 urllib.request

python - 人员列表中 2 人或 3 人的团体的所有组合 [Python]

Python 将 StringIO 转换为二进制

python - 文本文件在 AWS Elastic Beanstalk(Flask Web 应用程序)上无法正确显示

python - 连接两列

python - 使用选定值作为索引的 Pandas 数据透视表