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
?
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.
Use relocate() to change column positions, using the same syntax as select() to make it easy to move blocks of columns at once.
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 │
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With