# Query.jl, referencing \`\_.key\` in \`@groupby |\> @map\`

**URL:** https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318
**Category:** Data
**Tags:** queryverse
**Created:** [November 8, 2018, 7:13pm UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318 "2018-11-08T19:13:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [November 8, 2018, 7:13pm UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/1 "2018-11-08T19:13:13Z")

</div>

I followed the recording of the Queryverse tutorial at JuliaCon by @davidanthoff and now get errors when I use similar queries. In particular, I want to use the `_.key` shortcut in this query (see [full example](https://gist.github.com/leethargo/ba7ceeed1ff7fd8753a17a10ccc91759)):

```julia
data |>
    @groupby(_.symbol) |>
    @map({Symbol=_.key, Total=sum(_.price)})
--
type NamedTuple has no field key

```

I’m guessing the issue here is that I am using Julia 1.0 where `NamedTuple` is part of the language, while the tutorial was based on Julia 0.6 with `NamedTuple` from a 3rd party package?

In the [Gist](https://gist.github.com/leethargo/ba7ceeed1ff7fd8753a17a10ccc91759), I also wonder how to work around that issue.

---

<div class="post-metadata">

### Author: ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)
#### Post date: [November 8, 2018, 9:50pm UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/2 "2018-11-08T21:50:33Z")

</div>

Hi,

I noticed that you used `@map({Symbol =_.key, ....` and `key` doesn’t exist in the tuple, so just changing that to `@map({Symbol =_.symbol, ...` works.

As for a complete example, this those what you want

```julia
data |>
    @groupby(_.symbol) |>  
    @map({Symbol = _.symbol[1], Total=sum(_.price)})

```

---

<div class="post-metadata">

### Author: ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)
#### Post date: [November 8, 2018, 11:16pm UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/3 "2018-11-08T23:16:40Z")

</div>

This version also works.

```julia
x = data |>
    @groupby(_.symbol) |>  
    @map({ Symbol = key(_), Total=sum(_.price)})

```

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [November 9, 2018, 7:44am UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/4 "2018-11-09T07:44:20Z")

</div>

Yes, something like this is also what I came up with as a workaround.  
It’s still strange that the example from the tutorial would not work.

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [November 9, 2018, 7:44am UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/5 "2018-11-09T07:44:58Z")

</div>

Thanks, I did not know about this syntax, which is also nicer than `_.key`.

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [November 11, 2018, 8:43pm UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/6 "2018-11-11T20:43:18Z")

</div>

This is one of the breaking changes for the julia 1.0 version: you now generally have to access the key with the `key` function. That opens up the `.foo` syntax to access the columns of a group:

```julia
using Queryverse

df = DataFrame(A=[:a, :a, :b, :b], B=[1.,2.,3.,4.])

df |> @groupby(_.A) |> @map({A=key(_), B=sum(_.B)})

```

Note how I can now use `_.B` to access the whole `B` column of a given group `_`.

---

<div class="post-metadata">

### Author: ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)
#### Post date: [January 22, 2022, 10:30pm UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/7 "2022-01-22T22:30:52Z")

</div>

I could not make this work if grouping was done with more than one variable, like in `groupby(_.A, _.B)`, or even with a third column C.

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [January 22, 2022, 11:19pm UTC](https://discourse.julialang.org/t/query-jl-referencing-key-in-groupby-map/17318/8 "2022-01-22T23:19:14Z")

</div>

Here is how you do that:

```julia
df |>
@groupby({_.A, _.B, _.c}) |>
@map({key(_)..., D=sum(_.D), E=mean(_.E)})

```

or something like that.

The trick is to construct a new named tuple value by which things get grouped in the `@groupby` command, and then `key(_)` will be a named tuple itself.
