使用python 对 已知df['close'] 数据的情况下,求21日平均值数据:df['ma21']
时间: 2023-10-10 11:11:04 浏览: 122
使用pandas库可以很方便地对数据进行求解,可以通过rolling函数实现移动平均。具体代码如下:
```python
import pandas as pd
# 假设close列为已知的收盘价数据
df = pd.DataFrame({'close': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]})
# 计算21日平均值
df['ma21'] = df['close'].rolling(window=21).mean()
# 输出结果
print(df)
```
其中,rolling函数的参数window表示移动窗口的大小,这里设置为21。通过mean函数计算移动窗口内的平均值,赋值给新的列ma21。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)