# What is the DataFramesMeta way to specify a column by its name in a variable?

**URL:** <https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [March 14, 2020, 3:17am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954 "2020-03-14T03:17:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shg/32/207943_2.png) [@shg](https://discourse.julialang.org/u/shg)\
**Post date:** [March 14, 2020, 3:17am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954/1 "2020-03-14T03:17:26Z")

</div>

What is the proper way to specify a column by a string variable containing the column name in a DataFramesMeta query? I tried Symbol(var) but got an error.

```julia
julia> df = DataFrame(num = 1:3)
3×1 DataFrame
│ Row │ num │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 3 │

```

Specifying the column using the colon notation works.

```julia
julia> @where(df, :num .== 2)
1×1 DataFrame
│ Row │ num │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 2 │

```

But using Symbol(var) in place of the colon notation does not work. Is this an expected behavior?

```julia
julia> colname = "num"
"num"

julia> @where(df, Symbol(colname) .== 2)
ERROR: ArgumentError: invalid row index of type Bool
Stacktrace:
 [1] DataFrameRow at /Users/nishina/.julia/packages/DataFrames/S3ZFo/src/dataframerow/dataframerow.jl:59 [inlined
]
 [2] getindex(::DataFrame, ::Bool, ::Colon) at /Users/nishina/.julia/packages/DataFrames/S3ZFo/src/dataframerow/d
ataframerow.jl:92
 [3] where(::DataFrame, ::var"#92#93") at /Users/nishina/.julia/packages/DataFramesMeta/c88dH/src/DataFramesMeta.
jl:186
 [4] top-level scope at REPL[50]:1

julia> Symbol(colname) == :num
true

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 14, 2020, 3:33am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954/2 "2020-03-14T03:33:48Z")

</div>

As it is now, you need to use `cols`

```julia
colref = Symbol("num")
@where(df,cols(colref) .== 2)

```

---

<div class="post-metadata">

**Author:** ![shg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shg/32/207943_2.png) [@shg](https://discourse.julialang.org/u/shg)\
**Post date:** [March 14, 2020, 3:47am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954/3 "2020-03-14T03:47:46Z")

</div>

Thank you. It worked.

```julia
julia> @where(df, cols(Symbol(colname)) .== 2)
1×1 DataFrame
│ Row │ num │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 2 │

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 15, 2020, 7:18am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954/4 "2020-03-15T07:18:26Z")

</div>

You could also combine `@where` and `@with` in which case using the symbol works.

---

<div class="post-metadata">

**Author:** ![shg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shg/32/207943_2.png) [@shg](https://discourse.julialang.org/u/shg)\
**Post date:** [March 17, 2020, 2:57am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954/5 "2020-03-17T02:57:27Z")

</div>

crstnbr, sorry I don’t get it. Do you mean with @with I can rewrite the @where query without using cols?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 17, 2020, 8:41am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954/6 "2020-03-17T08:41:17Z")

</div>

I’m sorry, please ignore my comment, I’ve misread your question.

---

<div class="post-metadata">

**Author:** ![shg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shg/32/207943_2.png) [@shg](https://discourse.julialang.org/u/shg)\
**Post date:** [March 17, 2020, 10:20am UTC](https://discourse.julialang.org/t/what-is-the-dataframesmeta-way-to-specify-a-column-by-its-name-in-a-variable/35954/7 "2020-03-17T10:20:58Z")

</div>

All right. No problem.
