Q: Aggregated Dataframe with filters Is it possible in pandas to do a filter with the method "NamedAgg'?
Here is my example code:
df = pd.DataFrame({'Person': ['John','Paul','John','Paul','Taylor'],
                   'animal': ['cat', 'dog', 'cat', 'dog','dog'],
                   'from' : ['breeder','adoption','adoption','breeder','wild'],
                   'height': [9.1, 6.0, 9.5, 34.0,55],
                   'weight': [7.9, 7.5, 9.9, 198.0,200]})

df.groupby(['Person']).agg(
    number_of_animal = pd.NamedAgg(column = 'animal', aggfunc = 'count'),
    number_of_from = pd.NamedAgg(column = 'from', aggfunc = 'count'),
    total_height = pd.NamedAgg(column = 'height', aggfunc = 'sum'),
    total_weight = pd.NamedAgg(column = 'weight', aggfunc = 'sum')
    )

result = pd.DataFrame({'Person': ['John','Paul','Taylor'],
                        'number_of_animal':[2,0,0],
                        'number_of_from': [1,1,0],
                        'total_height':[0,34,55],
                        'total_weight':[17.8,205.5,200]})

For each individual column I would like to apply a filter, for example I would like to filter where for "number_of_animal" df['animal'] == 'cat' and "total_hight" df['height'] > 10 and number_of_from df['from'] == 'breeder

A: Use DataFrame.assign for reaasign replaced non matched values to NaNs in Series.where:
df1 = (df.assign(animal = df['animal'].where(df['animal'] == 'cat'),
                 height = df['height'].where(df['height'] > 10),
                 from1 = df['from'].where(df['from'] == 'breeder')
                )
        .groupby(['Person']).agg(
                 number_of_animal = pd.NamedAgg(column = 'animal', aggfunc = 'count'),
                 number_of_from = pd.NamedAgg(column = 'from1', aggfunc = 'count'),
                 total_height = pd.NamedAgg(column = 'height', aggfunc = 'sum'),
                 total_weight = pd.NamedAgg(column = 'weight', aggfunc = 'sum')
    ))
print (df1)
        number_of_animal  number_of_from  total_height  total_weight
Person                                                              
John                   2               1           0.0          17.8
Paul                   0               1          34.0         205.5
Taylor                 0               0          55.0         200.0

