Pandas: Group By A Column That Meets A Condition
I have a data set with three colums: rating , breed, and dog. import pandas as pd dogs = {'breed': ['Chihuahua', 'Chihuahua', 'Dalmatian', 'Sphynx'], 'dog': [True, True, Tr
Solution 1:
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.51 Dalmatian 10.0
Solution 2:
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
0False Sphynx 7.01True Chihuahua 8.52True Dalmatian 10.0print(res[res['dog']])
dog breed rating
1True Chihuahua 8.52True Dalmatian 10.0
Post a Comment for "Pandas: Group By A Column That Meets A Condition"