我有这样的代码:
y1 = data.groupby(['name', pd.Grouper(key='datetime', freq='15d')])['ext price'].mean()
y=pd.DataFrame(y1)
y.head()
输出为:
ext price
name datetime
Barton LLC 2014-01-01 07:21:51 3380.91
2014-01-16 07:21:51 399.5228571
2014-01-31 07:21:51 1666.523333
2014-02-15 07:21:51 1804.615
2014-03-02 07:21:51 1171.176667
我想以DataFrame的形式获取此输出,如下所示:
name datetime ext price
0 Barton LLC 2014-01-01 07:21:51 3380.91
1 Barton LLC 2014-01-16 07:21:51 399.5228571
2 Barton LLC 2014-01-31 07:21:51 1666.523333
3 Barton LLC 2014-02-15 07:21:51 1804.615
4 Barton LLC 2014-03-02 07:21:51 1171.176667
答案 0 :(得分:0)
使用reset_index
:
print(y.reset_index())
reset_index
只是建立新索引并将旧层级作为列,并使数据帧正常。
输出:
name datetime ext price
0 Barton LLC 2014-01-01 07:21:51 3380.91
1 Barton LLC 2014-01-16 07:21:51 399.5228571
2 Barton LLC 2014-01-31 07:21:51 1666.523333
3 Barton LLC 2014-02-15 07:21:51 1804.615
4 Barton LLC 2014-03-02 07:21:51 1171.176667