按布尔变量分组并使用每个组的结果创建一个新列 pandas

Group by a boolean variable and create a new column with the result for each group pandas

这可能有点令人困惑,但我有以下数据框:

exporter assets   liabilities

False      5          1
True       10         8
False      3          1
False      24         20
False      40         2
True       12         11

我想用这个公式计算比率df['liabilieties'].sum()/df['assets'].sum())*100

我希望创建一个新列,其中的值是比率,但针对每个布尔值进行计算,如下所示:

exporter assets   liabilities   ratio

False      5          1         33.3
True       10         8         86.3 
False      3          1         33.3
False      24         20        33.3
False      40         2         33.3
True       12         11        86.3

DataFrame.groupby on column exporter and transform the datafarme using sum, then use Series.div to divide liabilities by assets and use Series.mul乘以100:

d = df.groupby('exporter').transform('sum')
df['ratio'] = d['liabilities'].div(d['assets']).mul(100).round(2)

结果:

print(df)
   exporter  assets  liabilities  ratio
0     False       5            1  33.33
1      True      10            8  86.36
2     False       3            1  33.33
3     False      24           20  33.33
4     False      40            2  33.33
5      True      12           11  86.36