Skip to content

Flatten a list or string column

Source code

Description

[Deprecated]

$flatten() is deprecated. For Polars 2.0-compatible behavior, use $list$explode(empty_as_null = FALSE, keep_nulls = FALSE). To preserve the legacy behavior exactly, use $list$explode(empty_as_null = TRUE, keep_nulls = TRUE); the null handling differs between these forms.

Usage

<Expr>$flatten()

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  group = c("a", "b", "b"),
  values = list(1:2, 2:3, 4)
)

df$group_by("group")$agg(pl$col("values")$list$explode())
#> shape: (2, 2)
#> ┌───────┬─────────────────┐
#> │ group ┆ values          │
#> │ ---   ┆ ---             │
#> │ str   ┆ list[f64]       │
#> ╞═══════╪═════════════════╡
#> │ a     ┆ [1.0, 2.0]      │
#> │ b     ┆ [2.0, 3.0, 4.0] │
#> └───────┴─────────────────┘