# Group DataFrames by a function of a column

**URL:** <https://discourse.julialang.org/t/group-dataframes-by-a-function-of-a-column/32173>\
**Category:** Data\
**Tags:** package\
**Created:** [December 11, 2019, 9:45pm UTC](https://discourse.julialang.org/t/group-dataframes-by-a-function-of-a-column/32173 "2019-12-11T21:45:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [December 11, 2019, 9:45pm UTC](https://discourse.julialang.org/t/group-dataframes-by-a-function-of-a-column/32173/1 "2019-12-11T21:45:37Z")

</div>

I’m starting to use DataFrames more frequently and find myself often having to create a temporary table and column just to get a transformed column by which to group.

For example:

```julia
julia> d=DataFrame(x=rand(10^6));

julia> d[!,:g] = cut(d.x, 4);

julia> by(d, :g, :x => mean)
4×2 DataFrame
│ Row │ g │ x_mean │
│ │ Categorical… │ Float64 │
├─────┼───────────────────────┼──────────┤
│ 1 │ [2.55266e-7, 0.24991) │ 0.124957 │
│ 2 │ [0.24991, 0.500171) │ 0.375074 │
│ 3 │ [0.500171, 0.750146) │ 0.62502 │
│ 4 │ [0.750146, 0.999997] │ 0.875144 │

```

I often need to create binned statistics like this at the end of a chain of table transformations, so I’d prefer to be able to do everything in one line without the temp tables. Is there any way to acheive something like the following syntax? Is it reasonable to to make a feature request to support it?

```julia
julia> by(d, cut(:x, 4), :x => mean)

```

This syntax would be similar to what is possible in kdb+/q:

```julia
select avg x by 4 xrank x from d

```

---

<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:** [December 11, 2019, 10:07pm UTC](https://discourse.julialang.org/t/group-dataframes-by-a-function-of-a-column/32173/2 "2019-12-11T22:07:17Z")

</div>

this isn’t possible at the moment in DataFrames. You can file an issue that would make this easier, but note that your function always has to be evaluated, so unless `cut` only returns an iterator and not a vector, I would just write a small wrapper function to do this for you.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [December 11, 2019, 10:42pm UTC](https://discourse.julialang.org/t/group-dataframes-by-a-function-of-a-column/32173/3 "2019-12-11T22:42:27Z")

</div>

> [@robsmith11](#):
>
> by(d, cut(:x, 4), :x =\> mean)

this syntax would only be possible in a macro. Perhaps raise it with DataFramesMeta.jl.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [December 11, 2019, 11:08pm UTC](https://discourse.julialang.org/t/group-dataframes-by-a-function-of-a-column/32173/4 "2019-12-11T23:08:11Z")

</div>

> [@robsmith11](#):
>
> by(d, cut(:x, 4), :x =\> mean)

Another approach might be to just do this using a vector-based interface e.g. FastGroupBy.jl’s; and the `@df` macro. E.g.

```julia
using DataFrames

d = DataFrame(x = rand(1000))

using FastGroupBy, StatsPlots, CategoricalArrays

res = @df d fastby(mean, cut(:x, 4), :x) |> DataFrame

```

Just realised I left the “woohoo” in my code ☹ need to fix ASAP.

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [December 11, 2019, 11:29pm UTC](https://discourse.julialang.org/t/group-dataframes-by-a-function-of-a-column/32173/5 "2019-12-11T23:29:22Z")

</div>

It’s more than reasonable to request this as a feature.

Hmmm… I was doing this maybe 6-7 months ago through some sort of weird work around not using cut… Maybe I made my own iterator? I remember it not being too hard, but groking the latest code looks like things are different from what I remember… Maybe take a crack at making one? 😃

[https://github.com/JuliaData/DataFrames.jl/blob/025824f80e720693b7e21ac49d8ae64c8830ce98/src/groupeddataframe/grouping.jl#L165](https://github.com/JuliaData/DataFrames.jl/blob/025824f80e720693b7e21ac49d8ae64c8830ce98/src/groupeddataframe/grouping.jl#L165)
