# Change Column Names of a DataFrame - Previous methods don't work

**URL:** <https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 9, 2020, 10:49am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026 "2020-10-09T10:49:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [October 9, 2020, 10:49am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/1 "2020-10-09T10:49:48Z")

</div>

Apologies for the repeat of old question, but most of the posts on this question seem to give outdated/nonfunctional solutions for defining or updating the column names of a DataFrame.

DataFrame(datamat,names)  
or  
names!(df,names)  
rename!(df, names)

Aren’t working for me anymore.

I’ll keep searching, but what is the best approach?

Thanks in advance

PS In above ‘names is a vector of Symbols’. The statement worked in previous versions. Now I get the error

MethodError: no method matching rename!(::DataFrame, ::Array{Any,1})

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 9, 2020, 10:59am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/2 "2020-10-09T10:59:54Z")

</div>

```julia
julia> using DataFrames

julia> df=DataFrame(c1=[1,2,3],c2=[3,4,5])
3×2 DataFrame
│ Row │ c1 │ c2 │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 3 │
│ 2 │ 2 │ 4 │
│ 3 │ 3 │ 5 │

julia> rename!(df,:c1 => :a)
3×2 DataFrame
│ Row │ a │ c2 │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 3 │
│ 2 │ 2 │ 4 │
│ 3 │ 3 │ 5 │

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 9, 2020, 11:00am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/3 "2020-10-09T11:00:44Z")

</div>

```julia
julia> rename!(df,[:a,:b])
3×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 3 │
│ 2 │ 2 │ 4 │
│ 3 │ 3 │ 5 │

```

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [October 9, 2020, 11:02am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/4 "2020-10-09T11:02:43Z")

</div>

Thank you for this.

This latter is what I am after as my dataframe is too large to enumerate. I used this before with dataframe and Symbol Array, but now I get:

MethodError: no method matching rename!(::DataFrame, ::Array{Any,1})

[My name array contains only symbols]

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 9, 2020, 11:04am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/5 "2020-10-09T11:04:51Z")

</div>

```julia
help?> rename!
search: rename! rename propertynames

  rename!(df::AbstractDataFrame, vals::AbstractVector{Symbol};
          makeunique::Bool=false)
  rename!(df::AbstractDataFrame, vals::AbstractVector{<:AbstractString};
          makeunique::Bool=false)
...

```

Therefor an Array{Any,1} is not sufficient. It must be Array{Symbol,1} or Array{AbstractString,1}

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [October 9, 2020, 11:08am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/6 "2020-10-09T11:08:28Z")

</div>

Hi Thanks. I didn’t specify types, I just did what you did in your example  
[:a,:b] etc. but using a loop. Do I need to explicitly specify type?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 9, 2020, 11:09am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/7 "2020-10-09T11:09:51Z")

</div>

Try

```julia
symbols=Array{Symbol,1}(names)
rename!(df, symbols)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 9, 2020, 11:11am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/8 "2020-10-09T11:11:53Z")

</div>

Or show your code, so we can check where the `Any` comes from.  
This

```julia
julia> [:a,:c]
2-element Array{Symbol,1}:
 :a
 :c

```

is an array of Symbols. No need to be explicit with types.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [October 9, 2020, 11:20am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/9 "2020-10-09T11:20:35Z")

</div>

OK. Thanks. It seems that some of the column names weren’t Symbols and the previous version of DataFrames automatically converted them, the current one doesn’t.  
Thanks Alot!

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [March 5, 2021, 7:29am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/10 "2021-03-05T07:29:57Z")

</div>

It would be great if we could rename DataFrame columns like this:

```julia

rename!(data, [:oldname1, :oldname2] => [:newname1, :newname2] )

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 5, 2021, 9:00am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/11 "2021-03-05T09:00:25Z")

</div>

`rename!` accepts a vector of pairs, so you can do this:

```julia
rename!(data, [:oldname1, :oldname2] .=> [:newname1, :newname2])

```

---

<div class="post-metadata">

**Author:** ![mantzaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mantzaris/32/3852_2.png) [@mantzaris](https://discourse.julialang.org/u/mantzaris)\
**Post date:** [June 4, 2024, 11:09am UTC](https://discourse.julialang.org/t/change-column-names-of-a-dataframe-previous-methods-dont-work/48026/12 "2024-06-04T11:09:43Z")

</div>

I just tried `rename!( df, ["iter","voltage"] )` and it worked
