using names in such transforms is almost never needed. You can do just: julia> df = DataFrame(reshape(1:30, 3, 10), :auto) 3Γ—10 DataFrame Row β”‚ x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 β”‚ Int64 Int64 Int64 Int64 Int64 Int64 Int64 Int64 Int64 Int64 ─────┼─────… Answer from bkamins on discourse.julialang.org
🌐
Julia Programming Language
discourse.julialang.org β€Ί new to julia
DataFramesMeta selecting multiple columns - New to Julia - Julia Programming Language
November 16, 2020 - Using DataFramesMeta (or others), can I select multiple columns of choice and do some calculations? Here’s a mock data. I would like to subset by column x, and then calculate column-wise means. Here’s the answer. df = DataFrame(x = [1,1,1,2,3,2,3,2,3,], y1 = [2,1,2,1,2,1,2,1,2], y2 = ...
🌐
Juliadata
dataframes.juliadata.org β€Ί stable β€Ί man β€Ί working_with_dataframes
Working with DataFrames Β· DataFrames.jl
Finally, you can use Not, Between, Cols and All selectors in more complex column selection scenarios (note that Cols() selects no columns while All() selects all columns therefore Cols is a preferred selector if you write generic code). Here are examples of using each of these selectors: julia> df = DataFrame(r=1, x1=2, x2=3, y=4) 1Γ—4 DataFrame Row β”‚ r x1 x2 y β”‚ Int64 Int64 Int64 Int64 ─────┼──────────────────────────── 1 β”‚ 1 2 3 4 julia> df[:, Not(:r)] # drop :r column 1Γ—3 DataFrame Row β”‚ x1 x2 y β”‚ Int64 Int64 Int
🌐
Julia Bloggers
juliabloggers.com β€Ί column-selectors-in-dataframes-jl
Column selectors in DataFrames.jl | juliabloggers.com
You can put any valid other column selector inside Not: julia> df[:, Not("a")] 1Γ—5 DataFrame Row β”‚ b x1 x2 y1 y2 β”‚ Int64 Int64 Int64 Int64 Int64 ─────┼─────────────────────────────────── 1 β”‚ 2 3 4 5 6 julia> df[:, Not(r"x")] 1Γ—4 DataFrame Row β”‚ a b y1 y2 β”‚ Int64 Int64 Int64 Int64 ─────┼──────────────────────────── 1 β”‚ 1 2 5 6 julia> df[:, Not(Between(:x1, end))] 1Γ—2 DataFrame Row β”‚ a b β”‚ Int64 Int64 ─────┼────────────── 1 β”‚ 1 2
🌐
Juliadatascience
juliadatascience.io β€Ί select
Select - Julia Data Science
First, let’s create a dataset with multiple columns: function responses() id = [1, 2] q1 = [28, 61] q2 = [:us, :fr] q3 = ["F", "B"] q4 = ["B", "C"] q5 = ["A", "E"] DataFrame(; id, q1, q2, q3, q4, q5) end responses() Here, the data represents answers for five questions (q1, q2, …, q5) in a given questionnaire. We will start by β€œselecting” a few columns from this dataset. As usual, we use symbols to specify columns: ... Additionally, we can use Regular Expressions with Julia’s regex string literal.
Top answer
1 of 5
21

EDIT 2/7/2021: as people seem to still find this on Google, I'll edit this to say right at the top that current DataFrames (1.0+) allows both Not() selection supported by InvertedIndices.jl and also string types as column names, including regex selection with the r"" string macro. Examples:

julia> df = DataFrame(a1 = rand(2), a2 = rand(2), x1 = rand(2), x2 = rand(2), y = rand(["a", "b"], 2))
2Γ—5 DataFrame
 Row β”‚ a1        a2        x1        x2        y      
     β”‚ Float64   Float64   Float64   Float64   String 
─────┼────────────────────────────────────────────────
   1 β”‚ 0.784704  0.963761  0.124937  0.37532   a
   2 β”‚ 0.814647  0.986194  0.236149  0.468216  a

julia> df[!, r"2"]
2Γ—2 DataFrame
 Row β”‚ a2        x2       
     β”‚ Float64   Float64  
─────┼────────────────────
   1 β”‚ 0.963761  0.37532
   2 β”‚ 0.986194  0.468216

julia> df[!, Not(r"2")]
2Γ—3 DataFrame
 Row β”‚ a1        x1        y      
     β”‚ Float64   Float64   String 
─────┼────────────────────────────
   1 β”‚ 0.784704  0.124937  a
   2 β”‚ 0.814647  0.236149  a

Finally, the names function has a method which takes a type as its second argument, which is handy for subsetting DataFrames by the element type of each column:


julia> df[!, names(df, String)]
2Γ—1 DataFrame
 Row β”‚ y      
     β”‚ String 
─────┼────────
   1 β”‚ a
   2 β”‚ a

In addition to indexing with square brackets, there's also the select function (and its mutating equivalent select!), which basically takes the same input as the column index in []-indexing as its second argument:

julia> select(df, Not(r"a"))
2Γ—3 DataFrame
 Row β”‚ x1        x2        y      
     β”‚ Float64   Float64   String 
─────┼────────────────────────────
   1 β”‚ 0.124937  0.37532   a
   2 β”‚ 0.236149  0.468216  a

Original answer below


As @Reza Afzalan said, what you're trying to do returns an array of strings, while column names in DataFrames are symbols.

Given that Julia doesn't have conditional list comprehension, the nicest thing you could do I guess would be

data[:, filter(x -> x != :column1, names(df))]

This will give you the data set with column 1 removed (without mutating it). You could extend this to checking against lists of names as well:

data[:, filter(x -> !(x in [:column1,:column2]), names(df))]

UPDATE: As Ian says below, for this use case the Not syntax is now the best way to go.

More generally, conditional list comprehensions are also available by now, so you could do:

data[:, [x for x in names(data) if x != :column1]]
2 of 5
18

As of DataFrames 0.19, seems that you can now do

select(data, Not(:column1))

to select all but the column column1. To select all except for multiple columns, use an array in the inverted index:

select(data, Not([:column1, :column2]))

🌐
Juliadata
dataframes.juliadata.org β€Ί stable β€Ί man β€Ί getting_started
Getting Started Β· DataFrames.jl
The above operations did not work because when you use : as row selector the :B column is updated in-place, and it only supports storing strings. ... julia> df.B = df.B .== "F" 8-element BitVector: 0 1 1 0 1 0 0 1 julia> df 8Γ—3 DataFrame Row β”‚ A B C β”‚ Int64 Bool Int64 ─────┼───────────────────── 1 β”‚ 1 false 0 2 β”‚ 2 true 0 3 β”‚ 3 true 0 4 β”‚ 4 false 0 5 β”‚ 5 true 0 6 β”‚ 6 false 0 7 β”‚ 7 false 0 8 β”‚ 8 true 0
Find elsewhere
🌐
Bkamins
bkamins.github.io β€Ί julialang β€Ί 2023 β€Ί 08 β€Ί 18 β€Ί selectcolumn.html
DataFrames.jl survey: selecting columns of a data frame based on their values | Blog by BogumiΕ‚ KamiΕ„ski
August 18, 2023 - For example, assume that we want to pick columns that contain missing value. In this case the easiest way to do it is to use the eachcol(df) iterator over columns of our data frame: julia> select(df, any.(ismissing, eachcol(df))) 2Γ—2 DataFrame Row β”‚ a2 b1 β”‚ Int64?
🌐
Jkrumbiegel
jkrumbiegel.com β€Ί pages β€Ί 2021-12-28-new-features-dataframemacros
jkrumbiegel.com – Multi-columns, shortcut strings and subset transformations in DataFrameMacros.jl v0.2
December 28, 2021 - So far, DataFrameMacros.jl only supported statements with single-column specifiers. For example, @select(df, :x + 1) or @combine(df, $column_variable * $2). The expressions :x, $column_variable and $2 all refer to one column each. The underlying source-function-sink expression that DataFrameMacros.jl created was therefore always of the form source => function => sink.
🌐
Educative
educative.io β€Ί answers β€Ί how-to-select-a-subset-of-dataframe-columns-in-julia
How to select a subset of DataFrame columns in Julia
We can assign this new DataFrame to a separate variable named df. ... Let’s explain the code provided above. Lines 8–9: We use select() to subset the columns and assign the new DataFrame to a variable named df1 and then we print out df1.
🌐
Bkamins
bkamins.github.io β€Ί julialang β€Ί 2021 β€Ί 01 β€Ί 30 β€Ί bang.html
On the bang row selector in DataFrames.jl | Blog by BogumiΕ‚ KamiΕ„ski
January 30, 2021 - Note that for multiple column selection you can alternatively use the select function. The difference between select and indexing is that select returns a data frame even if a single column is selected, e.g. like this: julia> select(df, 1) 3Γ—1 DataFrame Row β”‚ col1 β”‚ Int64 ─────...
🌐
Julia School
julia.school β€Ί julia β€Ί dataframes
How to use dataframes in Julia
February 20, 2025 - To permanently delete the condition column, do this: ... 4Γ—3 DataFrame Row β”‚ item id kind β”‚ String Int64 String ─────┼─────────────────────────────────── 1 β”‚ Mars Rover 100 Rover 2 β”‚ Venus Explorer 101 Spaceship 3 β”‚ Lunar Rover 102 Rover 4 β”‚ 30% Sun Shade 103 Sun Shade Β· All we’re doing here is using the DataFrames package’s in-built function select!() toβ€”you guessed itβ€”select the columns we want.
🌐
Juliadata
juliadata.github.io β€Ί DataFrames.jl β€Ί stable β€Ί lib β€Ί functions
Functions Β· DataFrames.jl
The cols column selector can be any value accepted as column selector by the names function. Note that mapcols guarantees not to reuse the columns from df in the returned DataFrame. If f returns its argument then it gets copied before being stored. Metadata: this function preserves table-level and column-level :note-style metadata. ... julia> df = DataFrame(x=1:4, y=11:14) 4Γ—2 DataFrame Row β”‚ x y β”‚ Int64 Int64 ─────┼────────────── 1 β”‚ 1 11 2 β”‚ 2 12 3 β”‚ 3 13 4 β”‚ 4 14 julia> mapcols(x -> x.^2, df) 4Γ—2 DataFrame Row β”‚ x y β”‚ Int64 Int64 ─