# Grouping a DataFrame by something other than an existing column

**URL:** https://discourse.julialang.org/t/grouping-a-dataframe-by-something-other-than-an-existing-column/5246
**Category:** Data
**Tags:** data
**Created:** [August 6, 2017, 12:50am UTC](https://discourse.julialang.org/t/grouping-a-dataframe-by-something-other-than-an-existing-column/5246 "2017-08-06T00:50:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Seth\_Chandler](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Seth\_Chandler](https://discourse.julialang.org/u/Seth_Chandler)
#### Post date: [August 6, 2017, 12:50am UTC](https://discourse.julialang.org/t/grouping-a-dataframe-by-something-other-than-an-existing-column/5246/1 "2017-08-06T00:50:50Z")

</div>

Is it possible to group a DataFrame by something other than an existing column? Yes, I could add a column to the DataFrame that contains the computed values and then group on that new column. But this may clutter the DataFrame with columns that I use only once. Here’s some pseudo-syntax for what I would like to do if I were using the RDataset iris dataset and wanted to compute the mean petal width for irises depending on whether their sepal length was greater than 5.

```
 by(iris,iris[:SepalLength].>5.,df->mean(df[:PetalWidth]))

```

I have the sense that there has to be easy way to do this and I am just missing something.

Obviously I could do this

```
 iris[big_length]=iris[:SepalLength].>5.
 by(iris,:big_length,df->mean(df[:PetalWidth]))

```

But I am trying to avoid adding a new column to iris.

---

<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: [August 6, 2017, 4:20am UTC](https://discourse.julialang.org/t/grouping-a-dataframe-by-something-other-than-an-existing-column/5246/2 "2017-08-06T04:20:50Z")

</div>

You can do this with this [Query.jl](https://github.com/davidanthoff/Query.jl) query:

```julia
df = @from i in iris begin
    @group i.PetalWidth by i.SepalLength > 5 into g
    @select {big_length = g.key, PetalWidth = mean(g)}
    @collect DataFrame
end

```

---

<div class="post-metadata">

### Author: ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)
#### Post date: [August 6, 2017, 1:49pm UTC](https://discourse.julialang.org/t/grouping-a-dataframe-by-something-other-than-an-existing-column/5246/3 "2017-08-06T13:49:37Z")

</div>

Edit: nevermind, a new variable is indeed required…

Or [LazyQuery](https://github.com/bramtayl/LazyQuery.jl):

```julia
using RDatasets
using LazyQuery

@new_environment

@chain @evaluate begin
    using LazyQuery
    using RDatasets

    dataset("datasets", "iris")
    @add_to it BigLength = ~SepalLength > 5
    @group it by BigLength
    @make_from it PetalWidth = mean(PetalWidth)
    @ungroup it
end

```
