Skip to content

Explode columns containing a list of values

Source code

Description

Explode columns containing a list of values

Usage

<DataFrame>$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”).

Value

DataFrame

Examples

library("polars")

df = pl$DataFrame(
  letters = letters[1:4],
  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
#> shape: (4, 3)
#> ┌─────────┬─────────────────┬─────────────────┐
#> │ letters ┆ numbers         ┆ numbers_2       │
#> │ ---     ┆ ---             ┆ ---             │
#> │ str     ┆ list[f64]       ┆ list[f64]       │
#> ╞═════════╪═════════════════╪═════════════════╡
#> │ a       ┆ [1.0]           ┆ [0.0]           │
#> │ b       ┆ [2.0, 3.0]      ┆ [1.0, 2.0]      │
#> │ c       ┆ [4.0, 5.0]      ┆ [3.0, 4.0]      │
#> │ d       ┆ [6.0, 7.0, 8.0] ┆ [5.0, 6.0, 7.0] │
#> └─────────┴─────────────────┴─────────────────┘
# explode a single column, append others
df$explode("numbers")
#> shape: (8, 3)
#> ┌─────────┬─────────┬─────────────────┐
#> │ letters ┆ numbers ┆ numbers_2       │
#> │ ---     ┆ ---     ┆ ---             │
#> │ str     ┆ f64     ┆ list[f64]       │
#> ╞═════════╪═════════╪═════════════════╡
#> │ a       ┆ 1.0     ┆ [0.0]           │
#> │ b       ┆ 2.0     ┆ [1.0, 2.0]      │
#> │ b       ┆ 3.0     ┆ [1.0, 2.0]      │
#> │ c       ┆ 4.0     ┆ [3.0, 4.0]      │
#> │ c       ┆ 5.0     ┆ [3.0, 4.0]      │
#> │ d       ┆ 6.0     ┆ [5.0, 6.0, 7.0] │
#> │ d       ┆ 7.0     ┆ [5.0, 6.0, 7.0] │
#> │ d       ┆ 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")
#> shape: (8, 3)
#> ┌─────────┬─────────┬───────────┐
#> │ letters ┆ numbers ┆ numbers_2 │
#> │ ---     ┆ ---     ┆ ---       │
#> │ str     ┆ f64     ┆ f64       │
#> ╞═════════╪═════════╪═══════════╡
#> │ a       ┆ 1.0     ┆ 0.0       │
#> │ b       ┆ 2.0     ┆ 1.0       │
#> │ b       ┆ 3.0     ┆ 2.0       │
#> │ c       ┆ 4.0     ┆ 3.0       │
#> │ c       ┆ 5.0     ┆ 4.0       │
#> │ d       ┆ 6.0     ┆ 5.0       │
#> │ d       ┆ 7.0     ┆ 6.0       │
#> │ d       ┆ 8.0     ┆ 7.0       │
#> └─────────┴─────────┴───────────┘
df$explode(pl$col(pl$List(pl$Float64)))
#> shape: (8, 3)
#> ┌─────────┬─────────┬───────────┐
#> │ letters ┆ numbers ┆ numbers_2 │
#> │ ---     ┆ ---     ┆ ---       │
#> │ str     ┆ f64     ┆ f64       │
#> ╞═════════╪═════════╪═══════════╡
#> │ a       ┆ 1.0     ┆ 0.0       │
#> │ b       ┆ 2.0     ┆ 1.0       │
#> │ b       ┆ 3.0     ┆ 2.0       │
#> │ c       ┆ 4.0     ┆ 3.0       │
#> │ c       ┆ 5.0     ┆ 4.0       │
#> │ d       ┆ 6.0     ┆ 5.0       │
#> │ d       ┆ 7.0     ┆ 6.0       │
#> │ d       ┆ 8.0     ┆ 7.0       │
#> └─────────┴─────────┴───────────┘