Skip to content

Explode columns containing a list of values

Source code

Description

This will take every element of a list column and add it on an additional row.

Usage

<LazyFrame>$explode(...)

Arguments

Column(s) to be exploded as individual Into\ or list/vector of Into\. In a handful of places in rust-polars, only the plain variant Expr::Column is accepted. This is currenly one of such places. Therefore pl$col(“name”) and pl$all() is allowed, not pl$col(“name”)$alias(“newname”). “name” is implicitly converted to pl$col(“name”).

Details

Only columns of DataType List or Array can be exploded.

Named expressions like $explode(a = pl$col(“b”)) will not implicitly trigger $alias(“a”) here, due to only variant Expr::Column is supported in rust-polars.

Value

LazyFrame

Examples

library("polars")

df = pl$LazyFrame(
  letters = c("aa", "aa", "bb", "cc"),
  numbers = list(1, c(2, 3), c(4, 5), c(6, 7, 8)),
  numbers_2 = list(0, c(1, 2), c(3, 4), c(5, 6, 7)) # same structure as numbers
)
df
#> polars LazyFrame
#>  $explain(): Show the optimized query plan.
#> 
#> Naive plan:
#> DF ["letters", "numbers", "numbers_2"]; PROJECT */3 COLUMNS; SELECTION: None
# explode a single column, append others
df$explode("numbers")$collect()
#> shape: (8, 3)
#> ┌─────────┬─────────┬─────────────────┐
#> │ letters ┆ numbers ┆ numbers_2       │
#> │ ---     ┆ ---     ┆ ---             │
#> │ str     ┆ f64     ┆ list[f64]       │
#> ╞═════════╪═════════╪═════════════════╡
#> │ aa      ┆ 1.0     ┆ [0.0]           │
#> │ aa      ┆ 2.0     ┆ [1.0, 2.0]      │
#> │ aa      ┆ 3.0     ┆ [1.0, 2.0]      │
#> │ bb      ┆ 4.0     ┆ [3.0, 4.0]      │
#> │ bb      ┆ 5.0     ┆ [3.0, 4.0]      │
#> │ cc      ┆ 6.0     ┆ [5.0, 6.0, 7.0] │
#> │ cc      ┆ 7.0     ┆ [5.0, 6.0, 7.0] │
#> │ cc      ┆ 8.0     ┆ [5.0, 6.0, 7.0] │
#> └─────────┴─────────┴─────────────────┘
# explode two columns of same nesting structure, by names or the common dtype
# "List(Float64)"
df$explode("numbers", "numbers_2")$collect()
#> shape: (8, 3)
#> ┌─────────┬─────────┬───────────┐
#> │ letters ┆ numbers ┆ numbers_2 │
#> │ ---     ┆ ---     ┆ ---       │
#> │ str     ┆ f64     ┆ f64       │
#> ╞═════════╪═════════╪═══════════╡
#> │ aa      ┆ 1.0     ┆ 0.0       │
#> │ aa      ┆ 2.0     ┆ 1.0       │
#> │ aa      ┆ 3.0     ┆ 2.0       │
#> │ bb      ┆ 4.0     ┆ 3.0       │
#> │ bb      ┆ 5.0     ┆ 4.0       │
#> │ cc      ┆ 6.0     ┆ 5.0       │
#> │ cc      ┆ 7.0     ┆ 6.0       │
#> │ cc      ┆ 8.0     ┆ 7.0       │
#> └─────────┴─────────┴───────────┘
df$explode(pl$col(pl$List(pl$Float64)))$collect()
#> shape: (8, 3)
#> ┌─────────┬─────────┬───────────┐
#> │ letters ┆ numbers ┆ numbers_2 │
#> │ ---     ┆ ---     ┆ ---       │
#> │ str     ┆ f64     ┆ f64       │
#> ╞═════════╪═════════╪═══════════╡
#> │ aa      ┆ 1.0     ┆ 0.0       │
#> │ aa      ┆ 2.0     ┆ 1.0       │
#> │ aa      ┆ 3.0     ┆ 2.0       │
#> │ bb      ┆ 4.0     ┆ 3.0       │
#> │ bb      ┆ 5.0     ┆ 4.0       │
#> │ cc      ┆ 6.0     ┆ 5.0       │
#> │ cc      ┆ 7.0     ┆ 6.0       │
#> │ cc      ┆ 8.0     ┆ 7.0       │
#> └─────────┴─────────┴───────────┘