Julia: How to Rename Columns in A Data Frame

Hongtao Hao / 2021-07-05

The following codes run under Julia v1.6.1 and DataFrames v1.2.0.

To rename a column or columns, you need to use rename , or rename! . The difference is that rename creates a new data frame with the updated name(s) whereas rename! modifies the name(s) in-place.

If you have a lot of columns to rename, it might be better to use a Dict. I got this idea from a post by Sashank Bhogu.

julia> using DataFrames

julia> df = DataFrame(:A => [1, 2, 3], :B => ["a", "b", "c"])
3×2 DataFrame
 Row │ A      B      
     │ Int64  String 
─────┼───────────────
   1 │     1  a
   2 │     2  b
   3 │     3  c

julia> rename(df, :A => :C, :B => :D)
3×2 DataFrame
 Row │ C      D      
     │ Int64  String 
─────┼───────────────
   1 │     1  a
   2 │     2  b
   3 │     3  c

julia> df
3×2 DataFrame
 Row │ A      B      
     │ Int64  String 
─────┼───────────────
   1 │     1  a
   2 │     2  b
   3 │     3  c

julia> rename_dict = Dict(:A => :C, :B => :D)
Dict{Symbol, Symbol} with 2 entries:
  :A => :C
  :B => :D

julia> rename(df, rename_dict)
3×2 DataFrame
 Row │ C      D      
     │ Int64  String 
─────┼───────────────
   1 │     1  a
   2 │     2  b
   3 │     3  c

julia> rename!(df, :A => :C, :B => :D)
3×2 DataFrame
 Row │ C      D      
     │ Int64  String 
─────┼───────────────
   1 │     1  a
   2 │     2  b
   3 │     3  c

julia> df
3×2 DataFrame
 Row │ C      D      
     │ Int64  String 
─────┼───────────────
   1 │     1  a
   2 │     2  b
   3 │     3  c

Last modified on 2021-10-05