python - Pandas Group to Divide by Max -


i'm trying normalize user count dividing max users in each group. i'm able results calculate (commented out print works), i'm having trouble getting results save original table. code below doesn't throw error, doesn't add data weeklyperson:

weeklypersongroups=weeklyperson.groupby('person') personmax=weeklypersongroups['users'].max() name, group in weeklypersongroups:     #print(weeklyperson[weeklyperson['person']==name]['users']/personmax[name])     weeklyperson[weeklyperson['person']==name]['usersnorm']=weeklyperson[weeklyperson['person']==name]['users']/personmax[name] 

use groupby , transform

weeklyperson.groupby('person').users.transform(lambda x: x / x.max()) 

per @jeff's suggestion

weeklyperson.users / weeklyperson.groupby('person').users.transform(np.max) 

this avoids using lambda when isn't necessary.


Comments