I have a data set with three colums: rating , breed, and dog.
import pandas as pd
dogs = {\'breed\': [\'Chihuahua\', \'Chihuahua\', \'Dalmatian\', \'Sphynx\'
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