0

I have this code:

october_data_grouped = october_data.groupby(["Department", "Headcount", "Day"]).agg({'User ID': 'nunique'})
october_data_grouped.unstack(fill_value=0)

And this gives the output below:

enter image description here

Is there a way I can make a calculation that each individual entry is divided by the headcount [e.g. for row Accounts, day 1 would be 17% (6/35), day 4 would be 25% (9/35) etc.]

It_is_Chris
  • 13,504
  • 2
  • 23
  • 41

1 Answers1

0

Use MultiIndex.get_level_values with DataFrame.div:

#for avoid MultiIndex in columns
october_data_grouped = (october_data.groupby(["Department", "Headcount", "Day"])['User ID']
                                    .nunique()
                                    .unstack(fill_value=0))

idx = october_data_grouped.index.get_level_values('Headcount')
october_data_grouped = october_data_grouped.div(idx, axis=0)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252