econgineer econgineer - 10 months ago 50
Python Question

Pandas Group to Divide by Max

I'm trying to normalize a user count by dividing by the max users in each group. I'm able to get the results to calculate (commented out the print that works), but I'm having trouble getting the results to save back to the original table. The code below doesn't throw an error, but it also doesn't add any data to weeklyPerson:

for name, group in weeklyPersonGroups:


Use groupby and 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 it isn't necessary.