# Dynamic selection of columns Query.jl

**URL:** <https://discourse.julialang.org/t/dynamic-selection-of-columns-query-jl/79526>\
**Category:** Data\
**Tags:** query, dataframes, queryverse\
**Created:** [April 15, 2022, 10:43am UTC](https://discourse.julialang.org/t/dynamic-selection-of-columns-query-jl/79526 "2022-04-15T10:43:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davide-f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide-f/32/33780_2.png) [@davide-f](https://discourse.julialang.org/u/davide-f)\
**Post date:** [April 15, 2022, 10:43am UTC](https://discourse.julialang.org/t/dynamic-selection-of-columns-query-jl/79526/1 "2022-04-15T10:43:11Z")

</div>

Dear Community,

I am trying to dynamically select columns using Query.jl like in the following script, but I am unable to do so.  
Any ideas?

Thank you

list\_cols = [“col1”, “col2”…]

select = df |\> @select(∈(list\_cols)) |\> DataFrame

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [April 15, 2022, 11:08am UTC](https://discourse.julialang.org/t/dynamic-selection-of-columns-query-jl/79526/2 "2022-04-15T11:08:23Z")

</div>

Not an answer, but I give it for reference:

- If `df` is already a data frame you can just use `select(df, list_cols)`.
- If `df` is not a data frame then do `select!(DataFrame(df), list_cols)`. (`!` is used as we create a new data frame so it is safe to update it in place).

---

<div class="post-metadata">

**Author:** ![davide-f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide-f/32/33780_2.png) [@davide-f](https://discourse.julialang.org/u/davide-f)\
**Post date:** [April 15, 2022, 11:52am UTC](https://discourse.julialang.org/t/dynamic-selection-of-columns-query-jl/79526/3 "2022-04-15T11:52:24Z")

</div>

Thank you @bkamins, but I wanted to use the Query.jl framework, if possible

---

<div class="post-metadata">

**Author:** ![WileyQuixotey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wileyquixotey/32/33394_2.png) [@WileyQuixotey](https://discourse.julialang.org/u/WileyQuixotey)\
**Post date:** [October 16, 2023, 12:05pm UTC](https://discourse.julialang.org/t/dynamic-selection-of-columns-query-jl/79526/4 "2023-10-16T12:05:39Z")

</div>

I spent some time in this rabbit hole as well, and am closer to an answer, in the negative:

As of 2017, there was a technical constraint preventing the implementation of this. See the discussion [here](https://discourse.julialang.org/t/query-jl-automatize-multiple-select-at-once/4948).

I am not aware of any updates but would love to see them if there are any.

The cleanest way I currently know of is using bkamins’ answer up front and using Query.jl after that

```julia
using DataFrames
using Statistics
using Query
using RDatasets

df = dataset("datasets", "mtcars");

targets = [:MPG, :Cyl, :HP]

# list selection up front
df[:, targets] |>
  @filter(_.MPG > 15) |>
  @groupby(_.Cyl) |>
  @map({Cyl = key(_), AvgHP = mean(_.HP)})

```

As mentioned in the linked discussion, it might require doing things in two stages sometimes (ie, if the column list is a result of computation half-way down the pipe) but it’s totally workable.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 16, 2023, 2:56pm UTC](https://discourse.julialang.org/t/dynamic-selection-of-columns-query-jl/79526/5 "2023-10-16T14:56:25Z")

</div>

Not to plug a package I maintain too much, but DataFramesMeta.jl is very similar to Query.jl but includes dynamic column selection (with no performance drop)

```julia
mpg = :MPG
cyl = :Cyl
hp = :HP
@chain df begin
    @rsubset $mpg > 15
    groupby(cyl)
    @transform avghp = mean($hp)
end

```
