Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

groupby transfrom with lambda

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading