Julia DataFrame: remove column by name
dataframe, julia
Solution
As of Julia 1.0, you'll want to use `deletecols!`:
https://juliadata.github.io/DataFrames.jl/stable/lib/functions.html#DataFrames.deletecols!
julia> d = DataFrame(a=1:3, b=4:6)
3×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 4 │
│ 2 │ 2 │ 5 │
│ 3 │ 3 │ 6 │
julia> deletecols!(d, 1)
3×1 DataFrame
│ Row │ b │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 4 │
│ 2 │ 5 │
│ 3 │ 6 │
Problem
The DataFrame type in Julia allows you to access it as an array, so it is possible to remove columns via indexing: ``` df = df[:,[1:2,4:end]] # remove column 3 ``` The problem with this approach is that I often only know the column's name, not its column index in the table. Is there a built-in way to remove a column by name? Alternatively, is there a better way to do it than this? ``` colind = findfirst(names(df), colsymbol) df = df[:,[1:colind-1,colind+1:end]] ``` The above is failure prone; there are a few edge-cases (single column, first column, last column, symbol not in table, etc.) Thank you