Horizontally concatenate columns into a single list column
Description
Horizontally concatenate columns into a single list column
Usage
pl$concat_list(...)
Arguments
…
|
\<dynamic-dots \> Columns to concatenate into a single list
column. Accepts expression input. Strings are parsed as column names,
other non-expression inputs are parsed as literals.
|
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(a = list(1:2, 3, 4:5), b = list(4, integer(0), NULL))
# Concatenate two existing list columns. Null values are propagated.
df$with_columns(concat_list = pl$concat_list("a", "b"))
#> shape: (3, 3)
#> ┌────────────┬───────────┬─────────────────┐
#> │ a ┆ b ┆ concat_list │
#> │ --- ┆ --- ┆ --- │
#> │ list[f64] ┆ list[f64] ┆ list[f64] │
#> ╞════════════╪═══════════╪═════════════════╡
#> │ [1.0, 2.0] ┆ [4.0] ┆ [1.0, 2.0, 4.0] │
#> │ [3.0] ┆ [] ┆ [3.0] │
#> │ [4.0, 5.0] ┆ null ┆ null │
#> └────────────┴───────────┴─────────────────┘
# Non-list columns are cast to a list before concatenation. The output data
# type is the supertype of the concatenated columns.
df$select("a", concat_list = pl$concat_list("a", pl$lit("x")))
#> shape: (3, 2)
#> ┌────────────┬─────────────────────┐
#> │ a ┆ concat_list │
#> │ --- ┆ --- │
#> │ list[f64] ┆ list[str] │
#> ╞════════════╪═════════════════════╡
#> │ [1.0, 2.0] ┆ ["1.0", "2.0", "x"] │
#> │ [3.0] ┆ ["3.0", "x"] │
#> │ [4.0, 5.0] ┆ ["4.0", "5.0", "x"] │
#> └────────────┴─────────────────────┘
# Create lagged columns and collect them into a list. This mimics a rolling
# window.
df <- pl$DataFrame(A = c(1, 2, 9, 2, 13))
df <- df$select(
A_lag_1 = pl$col("A")$shift(1),
A_lag_2 = pl$col("A")$shift(2),
A_lag_3 = pl$col("A")$shift(3)
)
df$select(A_rolling = pl$concat_list("A_lag_1", "A_lag_2", "A_lag_3"))
#> shape: (5, 1)
#> ┌────────────────────┐
#> │ A_rolling │
#> │ --- │
#> │ list[f64] │
#> ╞════════════════════╡
#> │ [null, null, null] │
#> │ [1.0, null, null] │
#> │ [2.0, 1.0, null] │
#> │ [9.0, 2.0, 1.0] │
#> │ [2.0, 9.0, 2.0] │
#> └────────────────────┘