Skip to content

Take values by index

Description

Take values by index

Usage

<Expr>$gather(indices)

Arguments

indices An expression that leads to a UInt32 dtyped Series.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  group = c("one", "one", "one", "two", "two", "two"),
  value = c(1, 98, 2, 3, 99, 4)
)
df$group_by("group", maintain_order = TRUE)$agg(
  pl$col("value")$gather(c(2, 1))
)
#> shape: (2, 3)
#> ┌───────┬────────────────┬─────────────┐
#> │ group ┆ maintain_order ┆ value       │
#> │ ---   ┆ ---            ┆ ---         │
#> │ str   ┆ bool           ┆ list[f64]   │
#> ╞═══════╪════════════════╪═════════════╡
#> │ one   ┆ true           ┆ [2.0, 98.0] │
#> │ two   ┆ true           ┆ [4.0, 99.0] │
#> └───────┴────────────────┴─────────────┘