Skip to content

Rename column names of a LazyFrame

Source code

Description

Rename column names of a LazyFrame

Usage

<LazyFrame>$rename(...)

Arguments

One of the following:
  • Key value pairs that map from old name to new name, like old_name = “new_name”.
  • As above but with params wrapped in a list
  • An R function that takes the old names character vector as input and returns the new names character vector.

Details

If existing names are swapped (e.g. A points to B and B points to A), polars will block projection and predicate pushdowns at this node.

Value

LazyFrame

Examples

library("polars")

lf = pl$LazyFrame(
  foo = 1:3,
  bar = 6:8,
  ham = letters[1:3]
)

lf$rename(foo = "apple")$collect()
#> shape: (3, 3)
#> ┌───────┬─────┬─────┐
#> │ apple ┆ bar ┆ ham │
#> │ ---   ┆ --- ┆ --- │
#> │ i32   ┆ i32 ┆ str │
#> ╞═══════╪═════╪═════╡
#> │ 1     ┆ 6   ┆ a   │
#> │ 2     ┆ 7   ┆ b   │
#> │ 3     ┆ 8   ┆ c   │
#> └───────┴─────┴─────┘
lf$rename(
  \(column_name) paste0("c", substr(column_name, 2, 100))
)$collect()
#> shape: (3, 3)
#> ┌─────┬─────┬─────┐
#> │ coo ┆ car ┆ cam │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ str │
#> ╞═════╪═════╪═════╡
#> │ 1   ┆ 6   ┆ a   │
#> │ 2   ┆ 7   ┆ b   │
#> │ 3   ┆ 8   ┆ c   │
#> └─────┴─────┴─────┘