# Pull DataFrames columns to the front

**URL:** <https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [April 30, 2021, 3:54pm UTC](https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327 "2021-04-30T15:54:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [April 30, 2021, 3:54pm UTC](https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327/1 "2021-04-30T15:54:23Z")

</div>

I often need to pull certain DataFrames columns to the front and do this via `replace` with `Not`. I wonder if there is a more readable way to do it.

```julia
julia> using DataFrames

julia> df = DataFrame(D=[1], E=[2], C=[3], A=[4], B=[5])
1×5 DataFrame
 Row │ D E C A B
     │ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
   1 │ 1 2 3 4 5

julia> cols = [:A, :B];

julia> select(df, cols, Not(cols))
1×5 DataFrame
 Row │ A B D E C
     │ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
   1 │ 4 5 1 2 3

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [April 30, 2021, 3:58pm UTC](https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327/2 "2021-04-30T15:58:43Z")

</div>

[https://stackoverflow.com/questions/47694704/how-do-i-change-the-order-of-columns-in-a-julia-dataframe](https://stackoverflow.com/questions/47694704/how-do-i-change-the-order-of-columns-in-a-julia-dataframe)

answered by the author of DF, so yeah, what you’re doing is close to canonical already

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [April 30, 2021, 4:12pm UTC](https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327/3 "2021-04-30T16:12:41Z")

</div>

> [@jling](#):
>
> answered by the author of DF, so yeah, what you’re doing is close to canonical already

Unlike that question, I’m asking it more in general. What if you want `cols::Vector` to be placed in the front. Doing something with indexes is not really convenient. Even worse, what if you want the cols to be placed at position 2?

The reason that I ask is because `select(df, cols, Not(cols))` is not so readable and I’ve learned that whenever I need to make a function for something which seems like basic DataFrames functionality, I’m probably doing something wrong.

I would have expected something like `movecols!(df, 1, cols)` similar to `insertcols!`.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [April 30, 2021, 4:25pm UTC](https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327/4 "2021-04-30T16:25:34Z")

</div>

I find `select(df, cols, :)` quite readable. Similarly, to insert them at position 2 you can use `select(df, 1, cols, :)`. Or for position 3, `select(df, 1:2, cols, :)`.

---

<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:** [April 30, 2021, 4:29pm UTC](https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327/5 "2021-04-30T16:29:22Z")

</div>

Exactly as @sijo explains there is no need to use `Not`. In this case `:` is treated as every column except what was already included up to this point.

The general design goal of DataFrames.jl is to minimize the number of verbs the user has to learn so `select` and `select!` were judged to be enough as they should be flexible enough in most cases.

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [April 30, 2021, 4:40pm UTC](https://discourse.julialang.org/t/pull-dataframes-columns-to-the-front/60327/6 "2021-04-30T16:40:25Z")

</div>

> [@sijo](#):
>
> `select(df, 1, cols, :)`

I’m amazed!

> [@bkamins](#):
>
> In this case `:` is treated as every column except what was already included up to this point.

It never occurred to me that that was the case. Makes complete sense! Awesome design.

> [@bkamins](#):
>
> The general design goal of DataFrames.jl is to minimize the number of verbs the user has to learn so `select` and `select!` were judged to be enough as they should be flexible enough in most cases.

Now that I’ve seen `select(df, 1, cols, :)`, I completely agree.

Thanks for the responses, everyone
