Skip to content

Rename column names of a DataFrame

Source code

Description

Rename column names of a DataFrame

Usage

<DataFrame>$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

DataFrame

Examples

library("polars")

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

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