0

I have this multi-indexed DataFrame. The FGs are 4 groups I created.
I need to change the biomass (aka BIOMc) in percentages.
To do so I have to divide for the sum over biomasses inside each group. I don't know how to do that in a multi-index DataFrame.

I know how I can obtain the result for a single group, for example:

workdf.loc['RSH'] / workdf.loc['RSH'].sum()

But I don't know how to reiterate (without actually iterating because I don't think it's necessary here) the process for all the groups and without specifically writing the names of FGs.

import pandas as pd

workdf = pd.DataFrame({
    'FG': ['RSH', 'RSH', 'RSH', 'RSS', 'RSS', 'SSH', 'SSH', 'SSS', 'SSS', 'SSS'],
    'Diet': ['A', 'B', 'C', 'A', 'C', 'B', 'C', 'A', 'B', 'C'],
    'BIOMc': [3, 0, 21, 0, 2, 0, 11, 0, 1, 3]
}).set_index(['FG', 'Diet'])


          BIOMc
FG  Diet       
RSH A         3
    B         0
    C        21
RSS A         0
    C         2
SSH B         0
    C        11
SSS A         0
    B         1
    C         3
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Mirko
  • 115
  • 1
  • 9

1 Answers1

1

Use groupby+transform:

df['BIOMc']/df.groupby(level='FG')['BIOMc'].transform('sum')
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thanks a lot! Can you please explain shortly how .transform() works in this case? – Mirko Mar 19 '22 at 20:07
  • transform doesn't perform aggregation but replaces all values by the sum per group (try to run just `df.groupby(level='FG')['BIOMc'].transform('sum')`) – mozway Mar 19 '22 at 20:08