# Error changing the order of levels in a dataframe column

**URL:** <https://discourse.julialang.org/t/error-changing-the-order-of-levels-in-a-dataframe-column/98221>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [May 2, 2023, 8:34pm UTC](https://discourse.julialang.org/t/error-changing-the-order-of-levels-in-a-dataframe-column/98221 "2023-05-02T20:34:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgutig](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mgutig](https://discourse.julialang.org/u/mgutig)\
**Post date:** [May 2, 2023, 8:34pm UTC](https://discourse.julialang.org/t/error-changing-the-order-of-levels-in-a-dataframe-column/98221/1 "2023-05-02T20:34:02Z")

</div>

I have imported an excel spreadsheet to julia as a dataframe. After converting one of its columns into a categorical variable, I am trying to change the levels’ order but I can’t.

```julia
using DataFrames
using DataFramesMeta
using XLSX
using CategoricalArrays

```

`Data= DataFrame(XLSX.readtable("feedback.xlsx", "MG"))`

Once the dataframe has been created, I use DataFramesMeta to convert the column “expectations” into a categorical variable:

```julia
@transform(Data, :expectations = categorical(:expectations; ordered=true))
levels(Data.expectations)

```

5-element Vector{Any}:  
“Excellent”  
“Fair”  
“Good”  
“Poor”  
“Very good”

I would like to reorder the levels from Excellent to Poor: Excellent, Very good, Good, Fair, Poor.  
I thought the following line would work but it doesn´t:  
`levels!(Data.expectations, ["Excellent", "Very good", "Good", "Fair", "Poor"])`

ERROR: MethodError: no method matching levels!(::Vector{Any}, ::Vector{String})  
Closest candidates are:  
levels!(::CategoricalPool{S, R}, ::Vector; checkunique) where {S, R} at ~/.julia/packages/CategoricalArrays/tJ8hD/src/pool.jl:250  
levels!(::CategoricalArray{T, N, R}, ::AbstractVector; allowmissing, allow\_missing) where {T, N, R} at ~/.julia/packages/CategoricalArrays/tJ8hD/src/array.jl:793  
levels!(::SubArray{T, N, P}, ::Vector) where {T, N, P\<:CategoricalArray} at ~/.julia/packages/CategoricalArrays/tJ8hD/src/subarray.jl:5

What am I doing wrong?

Thank you.

---

<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:** [May 2, 2023, 9:28pm UTC](https://discourse.julialang.org/t/error-changing-the-order-of-levels-in-a-dataframe-column/98221/2 "2023-05-02T21:28:01Z")

</div>

> [@mgutig](#):
>
> `@transform`

Use `@transform!` or just do `Data.expectations = categorical(Data.expectations, ordered=true)` instead.

`@transform` allocates a fresh data frame.
