Home > Software engineering >  (Pandas) How to repeat item labels in pandas group by function?
(Pandas) How to repeat item labels in pandas group by function?

Time:06-30

I have the following data (df) :

enter image description here

I want to make a simple pivot (nested pivot like the following) where the labels are repeating like the yellow colored cells.

enter image description here

However, I am getting this

enter image description here

My code :

group_by = df.groupby(['meta_weight','meta_weight_type', 'weight_conversion'])['Sales_Unit'].sum()
print(group_by)

Please tell me how can I repeat the labels, as we do it in pivot table in excel. Thank you

CodePudding user response:

There is problem values are by default only not shown, so possible solution is turn off display this way:

with pd.option_context('display.multi_sparse', False):
     print(df)

CodePudding user response:

It got solved by resetting index values

group_by_meta = df.groupby(['meta_weight','meta_weight_type', 'weight_conversion'])['Sales_Unit'].sum()
print(group_by_meta)

group_by_meta = group_by_meta.reset_index()
  • Related