Reputation: 125
I have a dataset with week's worth of mobile pings. I want to know how many times on average do people go to gym, based on a group they're in, or based on where they're from.
The 'problem' is, some people never go to the gym, and I want to include those too, otherwise the average gets very skewed.
When I do a normal groupby though, 0 occurences aren't counted. I also need to include a condition that Ping_coordinates==Local_gym_coordinates.
What I want to get is:
What I've tried so far is:
df.groupby('ID').filter(lambda x: ((x['Ping_coordinates'] == x['Local_gym_coordinates']))).mean()
df.groupby('ID').filter(lambda x: ((x['Ping_coordinates'] == x['Local_gym_coordinates']).all()))
Upvotes: 0
Views: 37
Reputation: 323316
You can do
out1 = df['Ping_coordinates'].eq(df['Local_gym_coordinates']).groupby(df['ID']).sum()
out2 = out1.mean()
Upvotes: 1