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

Calculation between columns per group in pandas

Hello I have a dataframe such as :

COL1 COL2 COL3 
A    30   400
A    32   400
A    70   400
B    32   700
B    10   700

And I would like for each COL1, to calculate the sum of COL2 / COL3.unique()

Example = 30+32+70/400 = 0.33

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

Here I should then get:

COL1 COL2 COL3 New_col
A    30   400  0.33
A    32   400  0.33
A    70   400  0.33
B    32   700  0.06
B    10   700  0.06

Does someone have an idea please ?

>Solution :

In your case do groupby with transform

df['new'] = df.COL3.rdiv(df.groupby('COL1')['COL2'].transform('sum'))
df
Out[19]: 
  COL1  COL2  COL3   new
0    A    30   400  0.33
1    A    32   400  0.33
2    A    70   400  0.33
3    B    32   700  0.06
4    B    10   700  0.06
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