简体   繁体   中英

Python Pandas Don't Repeat Item Labels

I have a table: Table

How would I roll up Group, so that the group numbers don't repeat? I don't want to pd.df.groupby, as I don't want to summarize the other columns. I just want to not repeat item labels, sort of like an Excel pivot table.

Thanks!

In your dataframe it appears that 'Group' is in the index, the purpose of the index is to label each row. Therefore, is unusual and uncommon to have blank row indexes.

You you could so this:

df2.reset_index().set_index('Group', append=True).swaplevel(0,1,axis=0)

Or if you really must show blank row indexes you could do this, but you must change the dtype of the index to str.

df1 = df.set_index('Group').astype(str)
df1.index = df1.index.where(~df1.index.duplicated(),[' '])

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