# Way to give DataFrame columns nicknames?

**URL:** <https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [September 2, 2021, 1:41pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575 "2021-09-02T13:41:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [September 2, 2021, 1:41pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/1 "2021-09-02T13:41:26Z")

</div>

I am importing data from Excel into a DataFrame. The column headers are long specific names with spaces and units. They are a pain to work with because every time I need to access that column, I have to type `df."Long Specific Name With Spaces (Units)"`. Is there a good way to give the column a nickname like `:x`, so I can access it with `df.x` instead? The method would need to preserve the original headers, so I can transform them back for printing.

I think the correct tool is a dictionary, but I haven’t used them much. I also know that DataFrames.jl uses `=>` for some of its “split-apply-combine” syntax, and I don’t want to accidently interfere with that.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [September 2, 2021, 1:44pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/2 "2021-09-02T13:44:59Z")

</div>

How are you importing your data? If using `CSV.jl`, you can read using the `normalizenames` argument to get rid of the spaces and add underscores. Something like

```julia
 CSV.File(file; normalizenames=true)

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [September 2, 2021, 1:48pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/3 "2021-09-02T13:48:40Z")

</div>

Yes, I am using `CSV.jl`. That is good to know, but it is really the length of the name that is the problem.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 2, 2021, 2:18pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/4 "2021-09-02T14:18:59Z")

</div>

There’s no way to do this afaik, but it seems it would be quite easy to do

```julia
long_names = names(df)

short_names = [:x, :y, :z] # whatever you need

```

and then `rename!(df, short_names)` to work with the data with nicknames and `rename!(df, long_names)` again when producing outputs?

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [September 2, 2021, 2:22pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/5 "2021-09-02T14:22:55Z")

</div>

Adding to this, you can also provide change individual columns one at a time, for example ` rename!(df,"Long Name"=>"longname")`, or through a Dictionary if you want to change a series of column labels with `rename!(df,Dict("Long Name"=>"longname","Other Odd Name"=>"oddname"))`.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [September 2, 2021, 2:24pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/6 "2021-09-02T14:24:58Z")

</div>

Can you reverse the Dictionary to get the original names back?

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [September 2, 2021, 2:30pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/7 "2021-09-02T14:30:39Z")

</div>

Or if you are not sure whether the order or the number of columns might change, you can try the following approach:

```julia
short_names = string.(range('a', step = 1, length = size(d,2)))
dict1 = Dict(zip(Symbol.(names(d)), short_names))
dict2 = Dict(zip(Symbol.(short_names), names(d)))
rename!(d, dict1)
rename!(d, dict2)

```

where d is the DataFrame. Note that this might causes problems if you have more than 26 columns.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [September 2, 2021, 2:33pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/8 "2021-09-02T14:33:22Z")

</div>

You can reverse the Dictionary you used to rename everything, for example `reverse_dict = Dict(val=>key for (key,val) in dict)`.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 2, 2021, 2:40pm UTC](https://discourse.julialang.org/t/way-to-give-dataframe-columns-nicknames/67575/9 "2021-09-02T14:40:57Z")

</div>

You don’t need a Dict at all, just put the long and short names into two equal-length vectors and broadcast the pair operator in `rename!` to transparently go back between long and short names for the desired columns:

```julia
julia> df = DataFrame(["Long Name $i" => rand(2) for i ∈ 1:5]...)
2×5 DataFrame
 Row │ Long Name 1 Long Name 2 Long Name 3 Long Name 4 Long Name 5 
     │ Float64 Float64 Float64 Float64 Float64     
─────┼─────────────────────────────────────────────────────────────────
   1 │ 0.573031 0.0743141 0.187922 0.182269 0.251157
   2 │ 0.348444 0.986702 0.620114 0.881127 0.434446

julia> long_names = ["Long Name 1", "Long Name 3"];

julia> short_names = ["SN1", "SN3"]
2-element Vector{String}:
 "SN1"
 "SN3"

julia> rename!(df, long_names .=> short_names)
2×5 DataFrame
 Row │ SN1 Long Name 2 SN3 Long Name 4 Long Name 5 
     │ Float64 Float64 Float64 Float64 Float64     
─────┼───────────────────────────────────────────────────────────
   1 │ 0.573031 0.0743141 0.187922 0.182269 0.251157
   2 │ 0.348444 0.986702 0.620114 0.881127 0.434446

julia> rename!(df, short_names .=> long_names)
2×5 DataFrame
 Row │ Long Name 1 Long Name 2 Long Name 3 Long Name 4 Long Name 5 
     │ Float64 Float64 Float64 Float64 Float64     
─────┼─────────────────────────────────────────────────────────────────
   1 │ 0.573031 0.0743141 0.187922 0.182269 0.251157
   2 │ 0.348444 0.986702 0.620114 0.881127 0.434446

```
