Python 熊猫群比;聚合到原始数据帧中
原始数据帧是:Python 熊猫群比;聚合到原始数据帧中,python,pandas,dataframe,Python,Pandas,Dataframe,原始数据帧是: open high low close 2018-01-01 09:30:00 22.2 24.4 21.1 23.3 2018-01-01 09:35:00 . . . 2018-03-09 15:00:00 我想在日/周/月的基础上计算开/高/低/关,并将结果和相关指数(周、月)聚合到原始数据框中,多指数如下: open_day open_week open_month open
open high low close
2018-01-01 09:30:00 22.2 24.4 21.1 23.3
2018-01-01 09:35:00
.
.
.
2018-03-09 15:00:00
我想在日/周/月的基础上计算开/高/低/关,并将结果和相关指数(周、月)聚合到原始数据框中,多指数如下:
open_day open_week open_month open high low close
2018-Jan 1st-week 2018-01-01 09:30:00 22.2 24.4 21.1 23.3
09:35:00
.
.
.
听起来你需要
df.assign(open_month=df.index.strftime('%Y%B'),
open_week=df.index.strftime('%W'),
open_date=df.index.date,
open_time=df.index.time).\
set_index(['open_month','open_week','open_date','open_time'])
Out[235]:
open high low close
open_month open_week open_date open_time
2018January 01 2018-01-01 09:30:00 22.2 24.4 21.1 23.3
09:35:00 22.2 24.4 21.1 23.3
2018March 09 2018-03-01 09:35:00 22.2 24.4 21.1 23.3
如果您发布了一个您尝试过的代码示例,并描述了为什么它不能产生您预期的结果,那么您更有可能得到一个有用的答案。同意,但老实说,我仍然在起草代码以实现我想要的行为,而我无法想出任何代码。这就是为什么我只能在这里描述我的预期行为。@TianYan您需要重新采样您可以搜索Ohlc感谢您提供的提示,以进一步计算每天/每周/每月的开/高/低/关,并将其附加到原始数据框中。我将研究相似、重采样、ohlc等。。干杯