# Renaming multiple columns in Julia DataFrames

**URL:** <https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [June 20, 2022, 2:18pm UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072 "2022-06-20T14:18:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)\
**Post date:** [June 20, 2022, 2:18pm UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/1 "2022-06-20T14:18:08Z")

</div>

I have the following Julia Dataframe;

```julia
data_test = DataFrame(Time = 1:2, X1 = [0,1], X2 = [10,5])

```

And I have a list of names as follows;

```julia
technology = ["oil", "gas"]

```

How do I rename columns X1 and X2 using the list (excluding Time column). I can do it manually, however, this is not an efficient option to rename hundreds of columns. So, essentially, I’m what I’m looking for is a way to map the list of names to the columns. Any efficient solution is highly appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 20, 2022, 2:30pm UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/2 "2022-06-20T14:30:03Z")

</div>

One possible method is to use the high-level transforms of [TableTransforms.jl](https://github.com/JuliaML/TableTransforms.jl). For example, you can `Select` the columns of interest with a regular expression, and then `Rename` them:

```julia
pipeline = Select(r"X*") → Rename("X1" => "oil", "X2" => "gas", ...)

newdf = df |> pipeline

```

The transforms are implemented in terms of the Tables.jl API, we didn’t benchmark them against a DataFrames.jl-specific solution yet. Others can help with alternative solutions with different packages.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [June 20, 2022, 2:45pm UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/3 "2022-06-20T14:45:03Z")

</div>

Generalizing for an arbitrary number of columns using the built-in [`rename`](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.rename!) function from DataFrames.jl:

```julia
julia> rename!(data_test, ["X$i" => tech for (i, tech) in enumerate(technology)])
2×3 DataFrame
 Row │ Time oil gas
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 0 10
   2 │ 2 1 5

```

---

<div class="post-metadata">

**Author:** ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)\
**Post date:** [June 20, 2022, 3:04pm UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/4 "2022-06-20T15:04:54Z")

</div>

Thanks @stillyslalom …this is what I was looking for.

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [June 21, 2022, 12:40am UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/5 "2022-06-21T00:40:30Z")

</div>

One more alternative

```julia
@pipe replace.(names(data_test), names(data_test) .=> technology) |>
      rename!(data_test,_)

```

This method could come in handy if you need to use a regular expression. For example, if you want to change the names from X1,X2,X3… to Y1,Y2,Y3… you can do:

```julia
@pipe replace.(names(data_test), r"X" => "Y") |>
      rename!(data_test,_)

```

---

<div class="post-metadata">

**Author:** ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)\
**Post date:** [June 21, 2022, 3:53am UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/6 "2022-06-21T03:53:10Z")

</div>

use

```julia
rename!(data_test, names(data_test, r"X") .=> technology)

```

---

<div class="post-metadata">

**Author:** ![mreichMPI-BGC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mreichmpi-bgc/32/43775_2.png) [@mreichMPI-BGC](https://discourse.julialang.org/u/mreichMPI-BGC)\
**Post date:** [November 19, 2022, 2:43pm UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/7 "2022-11-19T14:43:48Z")

</div>

> [@alfaromartino](#):
>
> ```julia
> @pipe replace.(names(data_test), r"X" => "Y") |>
> rename!(data_test,_)
> 
> ```

Wouldn’t it be “better”/simpler to write

EDITED based on @alfaromartino correction of my omssion of the function argument…:

```julia
rename!(s -> replace(s, r"X" => "Y"), df)

```

?  
(Learning myself)…

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [November 20, 2022, 5:40am UTC](https://discourse.julialang.org/t/renaming-multiple-columns-in-julia-dataframes/83072/8 "2022-11-20T05:40:35Z")

</div>

Yes, that’s an alternative too. I just find splitting the two operations more readable, since the first line indicates the names you change and the second line that you use those names to rename the dataset (just a matter of preference).

Notice that if you want to use your option, you need to actually write:  
`rename!(s -> replace(s, r"X" => "Y"), df)`

(notice the `s` in `replace`)
