繁体   English   中英

如何通过跳过 DataFrame 中的不可用日期来获取 Python DataFrame 的三天高、低、收盘价?

[英]How do I Obtain Three days High,Low,Close of Python DataFrame below by skipping the not available Dates in DataFrame?

这是我正在研究的 DataFrame

 -      Date     Open    High    Low   Close
1. 01-08-2019 | 97.85 | 98.45 | 96.40 |97.25
2. 02-08-2019 | 97.15 | 98.95 | 96.75 |98.15
3. 05-08-2019 | 98.30 | 98.70 | 94.30 |95.65
4. 06-08-2019 | 95.75 | 97.75 | 95.20 |97.05
5. 07-08-2019 | 96.80 | 97.70 | 96.05 |96.90
6. 08-08-2019 | 97.40 | 98.90 | 96.55 |97.40
7. 09-08-2019 | 97.20 | 98.10 | 96.65 |97.30
8. 12-08-2019 | 97.20 | 97.25 | 93.40 |93.75
9. 13-08-2019 | 93.70 | 96.60 | 93.15 |96.35
- ................

运行此代码后:

df.set_index('Date').asfreq('D').resample('3D').agg({'High':'max','Low': 'max','Close':'max'})

计算的结果最大值为 01-08-2019、02-08-2019、03-08-2019。 但是在 2019 年 3 月 8 日,我在原始 DataFrame 中没有数据。 代码将 03-08-2019 为零但是我不想考虑 03-08-2019,而是希望我的代码通过将第三天作为 05-08-2019 来计算。

数据链接 [1]: https://drive.google.com/file/d/1OzRUHDMwh6xEp2ajdq3-rI5ddseGLhbV/view?usp=sharing

然后你不需要重新采样你的数据:

df.groupby(df.index // 3).agg({'High':'max','Low': 'max','Close':'max', 'Date': 'first'})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM