Skip to content

Rename columns of a DataFrame

Source code

Description

Rename columns of a DataFrame

Usage

<DataFrame>$rename(...)

Arguments

One of the following:
  • params like new_name = “old_name” to rename selected variables.
  • as above but with params wrapped in a list

Value

DataFrame

Examples

library(polars)

df = pl$DataFrame(mtcars)

df$rename(miles_per_gallon = "mpg", horsepower = "hp")
#> shape: (32, 11)
#> ┌──────────────────┬─────┬───────┬────────────┬───┬─────┬─────┬──────┬──────┐
#> │ miles_per_gallon ┆ cyl ┆ disp  ┆ horsepower ┆ … ┆ vs  ┆ am  ┆ gear ┆ carb │
#> │ ---              ┆ --- ┆ ---   ┆ ---        ┆   ┆ --- ┆ --- ┆ ---  ┆ ---  │
#> │ f64              ┆ f64 ┆ f64   ┆ f64        ┆   ┆ f64 ┆ f64 ┆ f64  ┆ f64  │
#> ╞══════════════════╪═════╪═══════╪════════════╪═══╪═════╪═════╪══════╪══════╡
#> │ 21.0             ┆ 6.0 ┆ 160.0 ┆ 110.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0  ┆ 4.0  │
#> │ 21.0             ┆ 6.0 ┆ 160.0 ┆ 110.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0  ┆ 4.0  │
#> │ 22.8             ┆ 4.0 ┆ 108.0 ┆ 93.0       ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> │ 21.4             ┆ 6.0 ┆ 258.0 ┆ 110.0      ┆ … ┆ 1.0 ┆ 0.0 ┆ 3.0  ┆ 1.0  │
#> │ 18.7             ┆ 8.0 ┆ 360.0 ┆ 175.0      ┆ … ┆ 0.0 ┆ 0.0 ┆ 3.0  ┆ 2.0  │
#> │ …                ┆ …   ┆ …     ┆ …          ┆ … ┆ …   ┆ …   ┆ …    ┆ …    │
#> │ 30.4             ┆ 4.0 ┆ 95.1  ┆ 113.0      ┆ … ┆ 1.0 ┆ 1.0 ┆ 5.0  ┆ 2.0  │
#> │ 15.8             ┆ 8.0 ┆ 351.0 ┆ 264.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 4.0  │
#> │ 19.7             ┆ 6.0 ┆ 145.0 ┆ 175.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 6.0  │
#> │ 15.0             ┆ 8.0 ┆ 301.0 ┆ 335.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 8.0  │
#> │ 21.4             ┆ 4.0 ┆ 121.0 ┆ 109.0      ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 2.0  │
#> └──────────────────┴─────┴───────┴────────────┴───┴─────┴─────┴──────┴──────┘
replacements = list(miles_per_gallon = "mpg", horsepower = "hp")
df$rename(replacements)
#> shape: (32, 11)
#> ┌──────────────────┬─────┬───────┬────────────┬───┬─────┬─────┬──────┬──────┐
#> │ miles_per_gallon ┆ cyl ┆ disp  ┆ horsepower ┆ … ┆ vs  ┆ am  ┆ gear ┆ carb │
#> │ ---              ┆ --- ┆ ---   ┆ ---        ┆   ┆ --- ┆ --- ┆ ---  ┆ ---  │
#> │ f64              ┆ f64 ┆ f64   ┆ f64        ┆   ┆ f64 ┆ f64 ┆ f64  ┆ f64  │
#> ╞══════════════════╪═════╪═══════╪════════════╪═══╪═════╪═════╪══════╪══════╡
#> │ 21.0             ┆ 6.0 ┆ 160.0 ┆ 110.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0  ┆ 4.0  │
#> │ 21.0             ┆ 6.0 ┆ 160.0 ┆ 110.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0  ┆ 4.0  │
#> │ 22.8             ┆ 4.0 ┆ 108.0 ┆ 93.0       ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> │ 21.4             ┆ 6.0 ┆ 258.0 ┆ 110.0      ┆ … ┆ 1.0 ┆ 0.0 ┆ 3.0  ┆ 1.0  │
#> │ 18.7             ┆ 8.0 ┆ 360.0 ┆ 175.0      ┆ … ┆ 0.0 ┆ 0.0 ┆ 3.0  ┆ 2.0  │
#> │ …                ┆ …   ┆ …     ┆ …          ┆ … ┆ …   ┆ …   ┆ …    ┆ …    │
#> │ 30.4             ┆ 4.0 ┆ 95.1  ┆ 113.0      ┆ … ┆ 1.0 ┆ 1.0 ┆ 5.0  ┆ 2.0  │
#> │ 15.8             ┆ 8.0 ┆ 351.0 ┆ 264.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 4.0  │
#> │ 19.7             ┆ 6.0 ┆ 145.0 ┆ 175.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 6.0  │
#> │ 15.0             ┆ 8.0 ┆ 301.0 ┆ 335.0      ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 8.0  │
#> │ 21.4             ┆ 4.0 ┆ 121.0 ┆ 109.0      ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 2.0  │
#> └──────────────────┴─────┴───────┴────────────┴───┴─────┴─────┴──────┴──────┘