The Python Oracle

Pandas: Group by a column that meets a condition

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor

--

Chapters
00:00 Pandas: Group By A Column That Meets A Condition
00:43 Accepted Answer Score 7
01:03 Answer 2 Score 1
01:25 Thank you

--

Full question
https://stackoverflow.com/questions/5066...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe #groupby #pandasgroupby

#avk47



ACCEPTED ANSWER

Score 7


Once you groupby and select a column, your dog column doesn't exist anymore in the context you have selected (and even if it did you are not accessing it correctly).

Filter your dataframe first, then use groupby with mean

df[df.dog].groupby('breed')['rating'].mean().reset_index()

       breed  rating
0  Chihuahua     8.5
1  Dalmatian    10.0



ANSWER 2

Score 1


An alternative solution is to make dog one of your grouper keys. Then filter by dog in a separate step. This is more efficient if you do not want to lose aggregated data for non-dogs.

res = df.groupby(['dog', 'breed'])['rating'].mean().reset_index()

print(res)

     dog      breed  rating
0  False     Sphynx     7.0
1   True  Chihuahua     8.5
2   True  Dalmatian    10.0

print(res[res['dog']])

    dog      breed  rating
1  True  Chihuahua     8.5
2  True  Dalmatian    10.0