# Write CSV with columns in a specific order

**URL:** <https://discourse.julialang.org/t/write-csv-with-columns-in-a-specific-order/55308>\
**Category:** General Usage\
**Tags:** dataframes, csv\
**Created:** [February 15, 2021, 11:24am UTC](https://discourse.julialang.org/t/write-csv-with-columns-in-a-specific-order/55308 "2021-02-15T11:24:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yogi1](https://avatars.discourse-cdn.com/v4/letter/y/439d5e/32.png) [@yogi1](https://discourse.julialang.org/u/yogi1)\
**Post date:** [February 15, 2021, 11:24am UTC](https://discourse.julialang.org/t/write-csv-with-columns-in-a-specific-order/55308/1 "2021-02-15T11:24:57Z")

</div>

Hi all,

I currently have some code which reads in a csv and does some manipulation and I want to then output my new dataframe in a specific order by column.

Currently I have column headers from the input:  
Country, Region, Currency, Capital

And i would like to write these out as:

Currency, Region, Currency, Capital

Is there a way to do this within the write function?

I tried reading in the columns as I wanted them output using select but that just wrote them in the same order they were in the input.

thanks in advance!

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [February 15, 2021, 11:42am UTC](https://discourse.julialang.org/t/write-csv-with-columns-in-a-specific-order/55308/2 "2021-02-15T11:42:56Z")

</div>

1. `select!(df, [:Currency, :Region, :Country, :Capital])`
2. Write DataFrame to CSV

taken from [How do I change the order of columns in a Julia DataFrame? - Stack Overflow](https://stackoverflow.com/a/47698731) after a 5 second internet search.  
Does this answer your question?

If you don’t want to replace the existing DataFrame, just use `select()` instead of `select!()` and pipe it into the CSV write function.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 15, 2021, 1:58pm UTC](https://discourse.julialang.org/t/write-csv-with-columns-in-a-specific-order/55308/3 "2021-02-15T13:58:21Z")

</div>

> [@fbanning](#):
>
> 1. `select!(df, [:Currency, :Region, :Country, :Capital])`
> 
> …
> 
> taken from [How do I change the order of columns in a Julia DataFrame? - Stack Overflow](https://stackoverflow.com/a/47698731) after a 5 second internet search.

> [@yogi1](#):
>
> I tried reading in the columns as I wanted them output using select but that just wrote them in the same order they were in the input.

…

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [February 15, 2021, 2:19pm UTC](https://discourse.julialang.org/t/write-csv-with-columns-in-a-specific-order/55308/4 "2021-02-15T14:19:37Z")

</div>

Sorry but the procedure just works as expected:

```julia
julia> using DataFrames

julia> df = DataFrame(a = rand(5), b = rand(5)) # create random df
5×2 DataFrame
 Row │ a b
     │ Float64 Float64
─────┼─────────────────────
   1 │ 0.51397 0.897642
   2 │ 0.242787 0.562512
   3 │ 0.733361 0.68564
   4 │ 0.0689889 0.59266
   5 │ 0.579379 0.527535

julia> select!(df, [:b, :a]) # inplace
5×2 DataFrame
 Row │ b a
     │ Float64 Float64
─────┼─────────────────────
   1 │ 0.897642 0.51397
   2 │ 0.562512 0.242787
   3 │ 0.68564 0.733361
   4 │ 0.59266 0.0689889
   5 │ 0.527535 0.579379

julia> select(df, [:a, :b]) # copy
5×2 DataFrame
 Row │ a b
     │ Float64 Float64
─────┼─────────────────────
   1 │ 0.51397 0.897642
   2 │ 0.242787 0.562512
   3 │ 0.733361 0.68564
   4 │ 0.0689889 0.59266
   5 │ 0.579379 0.527535

```

The resulting DataFrame can simply be used in the desired CSV write function then (also available via piping/chaining) and deliver the desired results.  
Maybe this more explicit example better helps OP to understand what has to be done.

---

<div class="post-metadata">

**Author:** ![yogi1](https://avatars.discourse-cdn.com/v4/letter/y/439d5e/32.png) [@yogi1](https://discourse.julialang.org/u/yogi1)\
**Post date:** [February 23, 2021, 3:17pm UTC](https://discourse.julialang.org/t/write-csv-with-columns-in-a-specific-order/55308/5 "2021-02-23T15:17:21Z")

</div>

Thanks for the replies both, I had done something silly with the dataframe and the select function worked fine!  
Will get this some day 🙂
