polars.dataframe.group_by.GroupBy.having#

GroupBy.having(*predicates: IntoExpr | Iterable[IntoExpr]) GroupBy[source]#

Filter groups with a list of predicates after aggregation.

Using this method is equivalent to adding the predicates to the aggregation and filtering afterwards.

This method can be chained and all conditions will be combined using &.

Parameters:
*predicates

Expressions that evaluate to a boolean value for each group. Typically, this requires the use of an aggregation function. Multiple predicates are combined using &.

Examples

Only keep groups that contain more than one element.

>>> df = pl.DataFrame(
...     {
...         "a": ["a", "b", "a", "b", "c"],
...     }
... )
>>> df.group_by("a").having(pl.len() > 1).agg()  
shape: (2, 1)
┌─────┐
│ a   │
│ --- │
│ str │
╞═════╡
│ b   │
│ a   │
└─────┘