I'd like help performing a groupby
per user in Python pandas. Here is some sample code to illustrate the example:
df = pd.DataFrame({'user': ['user1', 'user1',
'user2', 'user2'],
'condition': ['pl', 'md', 'pl', 'md'],
'value': [2, 4, 40, 42]})
df.groupby(['condition']).mean()
In this example, the average difference between users between the "pl" and "md" conditions is the same, but the aggregate difference is not.
How do I perform aggregate functions (mean, max, median) per user? I've looked at a few answers to this question but couldn't find an answer. I'd like to note that it may be possible to answer this specific question using the diff
function, but I'd like to run different types of aggregate functions, so learning how to groupby
per user more generally is my goal.
EDIT: one of the commenters suggested this solution
df.groupby(['user'])['value'].agg(['mean','max','median'])
And, I should have clarified: I'm looking for the aggregate difference given the difference between each user. This function creates a table of the mean and max for each other.
Rather, the mean function, for instance, should return a difference of 2.
Thanks for any help and let me know if I can make this question earlier to answer.