Julia DataFrame: remove column by name
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 │
Since delete!
throws a deprecation warning that suggests using select!
:
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> select!(d, Not(:a))
3×1 DataFrame
│ Row │ b │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 4 │
│ 2 │ 5 │
│ 3 │ 6 │
You can use select!
:
julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"], C = 2:5)
4x3 DataFrame
|-------|---|-----|---|
| Row # | A | B | C |
| 1 | 1 | "M" | 2 |
| 2 | 2 | "F" | 3 |
| 3 | 3 | "F" | 4 |
| 4 | 4 | "M" | 5 |
julia> select!(df, Not(:B))
4x2 DataFrame
|-------|---|---|
| Row # | A | C |
| 1 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 3 | 4 |
| 4 | 4 | 5 |
For more general ops, remember that you can pass an array of Symbols or a bool array too, and so arbitrarily complicated selections like
julia> df[~[(x in [:B, :C]) for x in names(df)]]
4x1 DataFrame
|-------|---|
| Row # | A |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
julia> df[setdiff(names(df), [:C])]
4x1 DataFrame
|-------|---|
| Row # | A |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
will also work.