我试图重新构造的大数据帧以下形式的多指标的:
date store_nbr item_nbr units snowfall preciptotal event
0 2012-01-01 1 1 0 0.0 0.0 0.0
1 2012-01-01 1 2 0 0.0 0.0 0.0
2 2012-01-01 1 3 0 0.0 0.0 0.0
3 2012-01-01 1 4 0 0.0 0.0 0.0
4 2012-01-01 1 5 0 0.0 0.0 0.0
我想组由store_nbr
(1-45)中,由store_nbr
(1-111),每个item_nbr
组内,然后对应的索引对(例如,store_nbr
= 12,item_nbr
= 109),按时间顺序显示的行,所以下令行的样子,例如:
store_nbr=12, item_nbr=109: date=2014-02-06, units=0, snowfall=...
date=2014-02-07, units=0, snowfall=...
date=2014-02-08, units=0, snowfall=...
... ...
store_nbr=12, item_nbr=110: date=2014-02-06, units=0, snowfall=...
date=2014-02-07, units=1, snowfall=...
date=2014-02-08, units=1, snowfall=...
...
它看起来像groupby
和set_index
的某种组合可能是有用的在这里,但下面这行后,我被卡住:
grouped = stores.set_index(['store_nbr', 'item_nbr'])
这将产生以下多指标:
date units snowfall preciptotal event
store_nbr item_nbr
1 1 2012-01-01 0 0.0 0.0 0.0
2 2012-01-01 0 0.0 0.0 0.0
3 2012-01-01 0 0.0 0.0 0.0
4 2012-01-01 0 0.0 0.0 0.0
5 2012-01-01 0 0.0 0.0 0.0
有没有人有这里的任何建议?有没有一种简单的方法通过操纵GROUPBY对象做到这一点?
您可以用行排序:
df.sort_values(by='date')