Skip to content

Get the first n values of a list

Source code

Description

Get the first n values of a list

Usage

<Expr>$list$head(n = 5L)

Arguments

n Number of values to return for each sublist. Can be an Expr. Strings are parsed as column names.

Value

Expr

Examples

library("polars")

df = pl$DataFrame(
  s = list(1:4, c(10L, 2L, 1L)),
  n = 1:2
)
df$with_columns(
  head_by_expr = pl$col("s")$list$head("n"),
  head_by_lit = pl$col("s")$list$head(2)
)
#> shape: (2, 4)
#> ┌─────────────┬─────┬──────────────┬─────────────┐
#> │ s           ┆ n   ┆ head_by_expr ┆ head_by_lit │
#> │ ---         ┆ --- ┆ ---          ┆ ---         │
#> │ list[i32]   ┆ i32 ┆ list[i32]    ┆ list[i32]   │
#> ╞═════════════╪═════╪══════════════╪═════════════╡
#> │ [1, 2, … 4] ┆ 1   ┆ [1]          ┆ [1, 2]      │
#> │ [10, 2, 1]  ┆ 2   ┆ [10, 2]      ┆ [10, 2]     │
#> └─────────────┴─────┴──────────────┴─────────────┘