我似乎无法弄清楚如何使用具有多级索引的列来旋转数据框。
我已经看过这个related post,但似乎无法弄清楚如何将其应用于我的问题。
如果我有df
:
df = pd.DataFrame.from_dict({('Mule Creek', 'ATI'): {5: 5.0, 6: 6.0, 7: 7.0, 8: 8.0, 9: 9.0},
('Mule Creek', 'ATIMR'): {5: 0.04115419564079352,
6: 0.04115419564079352,
7: 0.04115419564079352,
8: 0.04115419564079352,
9: 0.04115419564079352},
('Saddle Mtn.', 'ATI'): {5: 5.0, 6: 6.0, 7: 7.0, 8: 8.0, 9: 9.0},
('Saddle Mtn.', 'ATIMR'): {5: 0.05260285024705004,
6: 0.05260285024705004,
7: 0.05260285024705004,
8: 0.05260285024705004,
9: 0.05260285024705004},
('Calvert Creek', 'ATI'): {5: 5.0, 6: 6.0, 7: 7.0, 8: 8.0, 9: 9.0},
('Calvert Creek', 'ATIMR'): {5: 0.0068488368752893354,
6: 0.0068488368752893354,
7: 0.0068488368752893354,
8: 0.0068488368752893354,
9: 0.0068488368752893354}})
我想创建一个数据透视表,其中ATI
成为索引并使用`aggfunc ='mean'。
我尝试过:
df= df.unstack(level=-1)
df =df.pivot_table(values=tmp[0],index='level_0',columns='level_1',aggfunc=np.mean)
但是我得到了
KeyError: 1.0
所需结果:
ATI ATIMR
5 5.0 0.0335353
6 6.0 0.0335353
7 7.0 0.0335353
8 8.0 0.0335353
9 9.0 0.0335353
答案 0 :(得分:1)
这是您需要的吗?
newdf=df.stack(level=0).mean(level=0)
newdf
Out[886]:
ATI ATIMR
5 5.0 0.033535
6 6.0 0.033535
7 7.0 0.033535
8 8.0 0.033535
9 9.0 0.033535