'How to divide all values in column by level total in multi-indexed DataFrame [duplicate]
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
Solution 1:[1]
Use groupby+transform:
df['BIOMc']/df.groupby(level='FG')['BIOMc'].transform('sum')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
