繁体   English   中英

Pandas GroupBy 重置索引未重置

[英]Pandas GroupBy Reset Index not Resetting

我正在尝试重置我的数据帧上的索引,但 reset_index 并未完全重置列标题。 我将如何调整它以达到我想要的结果?

f = {'Price_x': ['count','mean','median', 'std', q1, q3]}
dfa = df.groupby(['Item','Criteria']).agg(f).reset_index()
print(list(dfa))

当前结果:

[('MLS#_y', ''), ('Criteria', ''), ('Price_x', 'count'), ('Price_x', 'mean'), ('Price_x', 'median'), ('Price_x', 'std'), ('Price_x', 'q1'), ('Price_x', 'q3')]

期望的结果:

['Item','Criteria','count','mean','median', ... ]

您将字典传递给agg<\/code> ,因此这将产生一个 MultiIndex。 如果您不希望这样,因为您只在单个列上使用所有这些函数,您可以直接使用:

dfa = df.groupby(['Item','Criteria'])['Price_x'].agg(['count','mean','median', 'std', q1, q3]).reset_index()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM