Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the order of columns in a Julia DataFrame?

Suppose I have a DataFrame like this:

julia> df = DataFrame(a = [1,2,3], b = [3,4,5])
3×2 DataFrames.DataFrame
│ Row │ a │ b │
├─────┼───┼───┤
│ 1   │ 1 │ 3 │
│ 2   │ 2 │ 4 │
│ 3   │ 3 │ 5 │

How do I subsequently change the order of columns so that column :b comes before column :a?

like image 508
Nicolas Payette Avatar asked Dec 07 '17 12:12

Nicolas Payette


People also ask

How do you reorder Dataframe columns?

You need to create a new list of your columns in the desired order, then use df = df[cols] to rearrange the columns in this new order.

How do I change the order of columns in Dplyr?

Use relocate() to change column positions, using the same syntax as select() to make it easy to move blocks of columns at once.


1 Answers

It's simple enough but it took a while to dawn on me so I thought I'd post it here:

julia> df = df[!, [:b, :a]]
3×2 DataFrames.DataFrame
│ Row │ b │ a │
├─────┼───┼───┤
│ 1   │ 3 │ 1 │
│ 2   │ 4 │ 2 │
│ 3   │ 5 │ 3 │
like image 158
Nicolas Payette Avatar answered Sep 29 '22 16:09

Nicolas Payette