How to rename several columns of Julia DataFrame?

Hello,
I have a dataframe and I would like to change all the names at once. I can change the name of a single column following this script, but what is the syntax for the whole lot?
I have tried giving an array of names as input and one as output, but did not work:

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, :c2] => [:a,:b])

ERROR: MethodError: no method matching rename!(::DataFrame, ::Vector{Pair{Vector{Symbol}, Vector{Symbol}}})
Closest candidates are:
  rename!(::AbstractDataFrame, ::AbstractVector{Symbol}; makeunique) at ~/.julia/packages/DataFrames/JZ7x5/src/abstractdataframe/abstractdataframe.jl:197
  rename!(::AbstractDataFrame, ::AbstractVector{<:AbstractString}; makeunique) at ~/.julia/packages/DataFrames/JZ7x5/src/abstractdataframe/abstractdataframe.jl:203
  rename!(::AbstractDataFrame, ::AbstractVector{Pair{Symbol, Symbol}}) at ~/.julia/packages/DataFrames/JZ7x5/src/abstractdataframe/abstractdataframe.jl:209
  ...
Stacktrace:
 [1] rename!(df::DataFrame, args::Pair{Vector{Symbol}, Vector{Symbol}})
   @ DataFrames ~/.julia/packages/DataFrames/JZ7x5/src/abstractdataframe/abstractdataframe.jl:235
 [2] top-level scope
   @ none:1

Very close! You just need to broadcast the pairs with .=>.

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

You could alternatively list each transformation sequentially:

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

If you want to change all the names you don’t actually have to map old to new or reference the old column names at all,

rename!(df, [:a, :b])

is enough

3 Likes