pandas groupby&聚合成原始数据帧

时间:2018-01-04 02:05:57

标签: 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  high  low   close
2018-Jan  1st-week   2018-01-01   09:30:00   22.2  24.4  21.1  23.3
                                  09:35:00
.
.
.

1 个答案:

答案 0 :(得分:1)

听起来像你需要

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
相关问题