简体   繁体   中英

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. 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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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