Q:

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?

A:

If you want to be minimally faster you can write
df[[2, 1]]

If you want to update df in place you can do it in two steps:
df[1], df[2] = df[2], df[1]
names!(df, [:b, :a])

which is yet faster.
EDIT: a new function permutecols! is provided by DataFrames.jl version 0.11.7 so that you can reorder columns in-place:
permutecols!(df, [:b, :a])

A:

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 │

