# Query.jl @groupby How to specify the key selector properly?

**URL:** <https://discourse.julialang.org/t/query-jl-groupby-how-to-specify-the-key-selector-properly/46929>\
**Category:** General Usage\
**Created:** [September 19, 2020, 7:31pm UTC](https://discourse.julialang.org/t/query-jl-groupby-how-to-specify-the-key-selector-properly/46929 "2020-09-19T19:31:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [September 19, 2020, 7:31pm UTC](https://discourse.julialang.org/t/query-jl-groupby-how-to-specify-the-key-selector-properly/46929/1 "2020-09-19T19:31:33Z")

</div>

Hello, I wonder what is the proper way to use the `@groupby` function from `Query.jl` when the key selector is a variable of type Symbol?

Example:

```julia
using DataFrames, Query
df = DataFrame(name=["John", "Sally", "Kirk"], age=[23., 42., 59.], children=[3,2,2])

x = df |> @groupby(_.children) |> collect
x

```

This gives

```julia
2-element Array{Grouping{Int64,NamedTuple{(:name, :age, :children),Tuple{String,Float64,Int64}}},1}:
 [(name = "John", age = 23.0, children = 3)]
 [(name = "Sally", age = 42.0, children = 2), (name = "Kirk", age = 59.0, children = 2)]

```

which is what I need. On the other hand, if I do

```julia
varname = Symbol("children")
y = df |> @groupby(varname) |> collect

```

Then I get

```julia
1-element Array{Grouping{Any,NamedTuple{(:name, :age, :children),Tuple{String,Float64,Int64}}},1}:
 [(name = "John", age = 23.0, children = 3), (name = "Sally", age = 42.0, children = 2), (name = "Kirk", age = 59.0, children = 2)]

```

which is different from above.

So what’s the proper way to use `groupby` if I’m writing a function that takes the input `varname`?

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [September 20, 2020, 5:19pm UTC](https://discourse.julialang.org/t/query-jl-groupby-how-to-specify-the-key-selector-properly/46929/2 "2020-09-20T17:19:33Z")

</div>

[This post](https://discourse.julialang.org/t/using-a-string-variable-in-a-query-jl-query/7372) looks related, but does not have a solution either.

Continuing the discussion from [Using a string variable in a Query.jl query](https://discourse.julialang.org/t/using-a-string-variable-in-a-query-jl-query/7372).
