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

By using either select or select! from DataFrames.jl. For example:

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

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 │

These are the recommendations for DataFrames.jl 0.21 or later.

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]
rename!(df, [:b, :a])

which is yet faster.

Also you can use select! like this:

select!(df, [:b, :a])