Home > Mobile >  groupby transfrom with lambda
groupby transfrom with lambda

Time:01-27

I have the following table:

df = pd.DataFrame({"A":['CH','CH','NU','NU','J'],
                   "B":['US','AU','Q','US','Q'],
                   "TOTAL":[10,13,3,1,18]})

enter image description here

And I wish to get the ratio of B with respect to its total for A. So the end result should be:

enter image description here

what I do is:

df['sum'] = df.groupby(['A'])['TOTAL'].transform(np.sum)
df['ratio'] = df['TOTAL']/df['sum']*100

Question: how can one achieve this with a lambda (or is there a better way).

CodePudding user response:

If you want to use a lambda you can do the division inside transform:

df['ratio'] = df.groupby('A')['TOTAL'].transform(lambda x: x / x.sum() * 100)

Output:

    A   B  TOTAL  sum       ratio
0  CH  US     10   23   43.478261
1  CH  AU     13   23   56.521739
2  NU   Q      3    4   75.000000
3  NU  US      1    4   25.000000
4   J   Q     18   18  100.000000

But this is slower (because we go group-by-group). If I were you, I'd choose your code over this one.

  •  Tags:  
  • Related