sumif in python on a column and create new column

Solutions on MaxInterview for sumif in python on a column and create new column by the best coders in the world

showing results for - "sumif in python on a column and create new column"
Delfina
28 Feb 2020
1df['treat_rate']=df.treats/df.groupby('pet').treats.transform('sum')
2df
3Out[153]: 
4    pet  treats  lbs  treat_rate
50   cat       2  5.0        0.25
61   dog       1  9.9        0.50
72  snek       3  1.1        1.00
83   cat       6  4.5        0.75
94   dog       1  9.4        0.50