polars.Expr.flatten#
- Expr.flatten() Expr[source]#
Flatten a list or string column.
Alias for
Expr.list.explode().Deprecated since version 1.38:
Expr.flatten()is deprecated and will be removed in version 2.0. UseExpr.list.explode(keep_nulls=False, empty_as_null=False)instead, which provides the behavior you likely expect.Examples
>>> df = pl.DataFrame( ... { ... "group": ["a", "b", "b"], ... "values": [[1, 2], [2, 3], [4]], ... } ... ) >>> df.group_by("group").agg(pl.col("values").flatten()) shape: (2, 2) ┌───────┬───────────┐ │ group ┆ values │ │ --- ┆ --- │ │ str ┆ list[i64] │ ╞═══════╪═══════════╡ │ a ┆ [1, 2] │ │ b ┆ [2, 3, 4] │ └───────┴───────────┘