polars.Expr.list.agg#

Expr.list.agg(expr: Expr) Expr[source]#

Run any polars aggregation expression against the lists’ elements.

Parameters:
expr

Expression to run. Note that you can select an element with pl.element().

See also

polars.Expr.list.eval

Evaluates expressions without automatically exploding.

polars.Expr.arr.agg

Same for the Array datatype.

Examples

>>> df = pl.DataFrame({"a": [[1, None], [42, 13], [None, None]]})
>>> df.with_columns(null_count=pl.col.a.list.agg(pl.element().null_count()))
shape: (3, 2)
┌──────────────┬────────────┐
│ a            ┆ null_count │
│ ---          ┆ ---        │
│ list[i64]    ┆ u32        │
╞══════════════╪════════════╡
│ [1, null]    ┆ 1          │
│ [42, 13]     ┆ 0          │
│ [null, null] ┆ 2          │
└──────────────┴────────────┘
>>> df.with_columns(no_nulls=pl.col.a.list.agg(pl.element().drop_nulls()))
shape: (3, 2)
┌──────────────┬───────────┐
│ a            ┆ no_nulls  │
│ ---          ┆ ---       │
│ list[i64]    ┆ list[i64] │
╞══════════════╪═══════════╡
│ [1, null]    ┆ [1]       │
│ [42, 13]     ┆ [42, 13]  │
│ [null, null] ┆ []        │
└──────────────┴───────────┘