Skip to content

Join elements of a list

Source code

Description

Join all string items in a sublist and place a separator between them. This only works on columns of type list[str].

Usage

<Expr>$list$join(separator, ignore_nulls = FALSE)

Arguments

separator String to separate the items with. Can be an Expr. Strings are not parsed as columns.
ignore_nulls If FALSE (default), null values are propagated: if the row contains any null values, the output is null.

Value

Expr

Examples

library("polars")

df = pl$DataFrame(
  s = list(c("a", "b", "c"), c("x", "y"), c("e", NA)),
  separator = c("-", "+", "/")
)
df$with_columns(
  join_with_expr = pl$col("s")$list$join(pl$col("separator")),
  join_with_lit = pl$col("s")$list$join(" "),
  join_ignore_null = pl$col("s")$list$join(" ", ignore_nulls = TRUE)
)
#> shape: (3, 5)
#> ┌─────────────────┬───────────┬────────────────┬───────────────┬──────────────────┐
#> │ s               ┆ separator ┆ join_with_expr ┆ join_with_lit ┆ join_ignore_null │
#> │ ---             ┆ ---       ┆ ---            ┆ ---           ┆ ---              │
#> │ list[str]       ┆ str       ┆ str            ┆ str           ┆ str              │
#> ╞═════════════════╪═══════════╪════════════════╪═══════════════╪══════════════════╡
#> │ ["a", "b", "c"] ┆ -         ┆ a-b-c          ┆ a b c         ┆ a b c            │
#> │ ["x", "y"]      ┆ +         ┆ x+y            ┆ x y           ┆ x y              │
#> │ ["e", null]     ┆ /         ┆ null           ┆ null          ┆ e                │
#> └─────────────────┴───────────┴────────────────┴───────────────┴──────────────────┘