简体   繁体   English

Pandas GroupBy 重置索引未重置

[英]Pandas GroupBy Reset Index not Resetting

I am trying to reset the index on my dataframe, but the reset_index is not fully resetting the column headers.我正在尝试重置我的数据帧上的索引,但 reset_index 并未完全重置列标题。 How would I adjust this to reach my desired outcome?我将如何调整它以达到我想要的结果?

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

Current result:当前结果:

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

Desired Result:期望的结果:

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

You're passing a dictionary to agg<\/code> , so this will result in a MultiIndex.您将字典传递给agg<\/code> ,因此这将产生一个 MultiIndex。 If you didn't want that, since you're only using all these functions on a single column, you directly use:如果您不希望这样,因为您只在单个列上使用所有这些函数,您可以直接使用:

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