Uncategorized

Pandas Groupby apply Function on each Group Item

Here we are trying to write a solution for

Group by COULUMN_2

Sum the COULUMN_1 values in each group and divide each COULUMN_1 values by Sum of it’s group total.

# using transform function
df_new = df[['COULUMN_1','COULUMN_2']]
grp = df_new.groupby('COULUMN_2') 
sc = lambda x: (x) / x.sum() # sum the COULUMN_1 values in each group and divide each COULUMN_1 values by Sum of it's group total.
df_new['COULUMN_1'] = grp.transform(sc)

After that we need to do

df[‘COULUMN_1’] = df_new[‘COULUMN_2’]

Where for example column 1 is RANK and column 2 is Id

Thanks

Leave a Reply

%d bloggers like this: