# DataFrames reorder columns in place

**URL:** <https://discourse.julialang.org/t/dataframes-reorder-columns-in-place/67993>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [September 10, 2021, 4:58pm UTC](https://discourse.julialang.org/t/dataframes-reorder-columns-in-place/67993 "2021-09-10T16:58:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjanun](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mjanun](https://discourse.julialang.org/u/mjanun)\
**Post date:** [September 10, 2021, 4:58pm UTC](https://discourse.julialang.org/t/dataframes-reorder-columns-in-place/67993/1 "2021-09-10T16:58:14Z")

</div>

If a dataframe is defined as:  
` df_dummy = DataFrame(A=[1,2,3],B=["A","B","C"],ID =[121,321,421])`  
How can I change the order of one or more columns and modify the dataframe in place? If I want to make ID the first column and B the last column.

---

<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 10, 2021, 9:08pm UTC](https://discourse.julialang.org/t/dataframes-reorder-columns-in-place/67993/2 "2021-09-10T21:08:11Z")

</div>

```julia
julia> df_dummy = DataFrame(A=[1,2,3],B=["A","B","C"],ID =[121,321,421])
3×3 DataFrame
 Row │ A B ID
     │ Int64 String Int64
─────┼──────────────────────
   1 │ 1 A 121
   2 │ 2 B 321
   3 │ 3 C 421

julia> select!(df_dummy, :ID, Not([:ID, :B]), :B)
3×3 DataFrame
 Row │ ID A B
     │ Int64 Int64 String
─────┼──────────────────────
   1 │ 121 1 A
   2 │ 321 2 B
   3 │ 421 3 C

```

Note that `select!(df_dummy, :ID, Not(:B), :B)` is a bit shorter and would also work because the selection is evaluated left to right. But I think explicitly excluding `:ID` is more clear in conveying the intention.

Similarly if you knew you have exactly 3 columns you can just write `select!(df_dummy, :ID, :A, :B)`. The expression I have given above is general and would allow any number of columns in the middle.

---

<div class="post-metadata">

**Author:** ![mjanun](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mjanun](https://discourse.julialang.org/u/mjanun)\
**Post date:** [September 11, 2021, 5:08pm UTC](https://discourse.julialang.org/t/dataframes-reorder-columns-in-place/67993/4 "2021-09-11T17:08:57Z")

</div>

Thank you. I use `select` a lot but never realised it can be used this way

---

<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 11, 2021, 7:18pm UTC](https://discourse.julialang.org/t/dataframes-reorder-columns-in-place/67993/5 "2021-09-11T19:18:48Z")

</div>

We try to minimize the number of functions that users need to learn as they are already quite complex 😃.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 13, 2021, 11:05am UTC](https://discourse.julialang.org/t/dataframes-reorder-columns-in-place/67993/6 "2021-09-13T11:05:26Z")

</div>

I don’t know if it is exactly the columns permutation you are looking for, but it could be an equivalent way of achieving the same result

```julia
select!(df_dummy,circshift(names(df_dummy),1))
#or 
df_dummy=df_dummy[:,circshift(names(df_dummy),1)]

```
