Skip to main content

Filtering by the result of an aggregate function

This is my dataframe:

        date    id      value
0   2002-01-07  PA12165 119.63
1   2002-03-13  PA12165 119.48
2   2002-05-13  PA12165 114.50
3   2002-06-12  PA12165 120.27
4   2002-06-12  PA12165 120.27
...

I want to get another dataframe by filtering by the result of an aggregate function. This is my attempt:

my_aggs = {'id': 'count',
           'value': [min, max, 'mean']
          }
df.groupby(['id', 'date']).agg(my_aggs).query('count > 1')

raises: UndefinedVariableError: name 'count' is not defined

As you can see I can't find the way to reference my aggregate function in the query



source https://stackoverflow.com/questions/75784369/filtering-by-the-result-of-an-aggregate-function

Comments