lala345
lala345

Reputation: 125

Groupby with condition, include 0 occurrences

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.

enter image description here

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:

enter image description here

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

Answers (1)

BENY
BENY

Reputation: 323316

You can do

out1 = df['Ping_coordinates'].eq(df['Local_gym_coordinates']).groupby(df['ID']).sum()

out2 = out1.mean()

Upvotes: 1

Related Questions