# How to "label" DataFrame categorical column?

**URL:** <https://discourse.julialang.org/t/how-to-label-dataframe-categorical-column/67790>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [September 7, 2021, 6:58am UTC](https://discourse.julialang.org/t/how-to-label-dataframe-categorical-column/67790 "2021-09-07T06:58:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [September 7, 2021, 6:58am UTC](https://discourse.julialang.org/t/how-to-label-dataframe-categorical-column/67790/1 "2021-09-07T06:58:02Z")

</div>

How can I rename the levels of a categorical column in a `DataFrame`? I tried this but it throws an error:

```julia
using DataFrames
using CategoricalArrays

df = DataFrame(x=[1, 2, 3])
df.x = categorical(df.x)
levels!(df.x, ["one", "two", "three"])

ERROR: LoadError: ArgumentError: cannot remove level 1 as it is used at position 1. Change the array element type to Union{Int64, Missing} using convert if you want to transform some levels to missing values.

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 7, 2021, 7:35am UTC](https://discourse.julialang.org/t/how-to-label-dataframe-categorical-column/67790/2 "2021-09-07T07:35:01Z")

</div>

You can do e.g. (to show the use of DataFramesMeta.jl):

```julia
julia> using DataFramesMeta

julia> @transform(df, :x = recode(:x, 1 => "one", 2 => "two", 3 => "tree"))
3×1 DataFrame
 Row │ x
     │ Cat…
─────┼──────
   1 │ one
   2 │ two
   3 │ tree

```

the point is that you cannot replace integers with strings in place as it would change the element type of the container. You need to create a new recoded categorical vector.

If you want just to get the vector use:

```julia
julia> recode(df.x, 1 => "one", 2 => "two", 3 => "tree")
3-element CategoricalArray{String,1,UInt32}:
 "one"
 "two"
 "tree"

```
