# DataFrame Groupby

**URL:** <https://discourse.julialang.org/t/dataframe-groupby/10544>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [April 25, 2018, 6:25pm UTC](https://discourse.julialang.org/t/dataframe-groupby/10544 "2018-04-25T18:25:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdmcallister](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdmcallister/32/2324_2.png) [@sdmcallister](https://discourse.julialang.org/u/sdmcallister)\
**Post date:** [April 25, 2018, 6:25pm UTC](https://discourse.julialang.org/t/dataframe-groupby/10544/1 "2018-04-25T18:25:53Z")

</div>

Is there a way to do something similar in DataFrames?

```julia
julia> t=table([1,1,1,2,2,2], [1,1,2,2,1,1], [1,2,3,4,5,6],
               names=[:x,:y,:z]);

julia> groupby(identity, t, (:x, :y), select=:z)
Table with 4 rows, 3 columns:
x y identity
──────────────
1 1 [1, 2]
1 2 [3]
2 1 [5, 6]
2 2 [4]

```

Thank you.

---

<div class="post-metadata">

**Author:** ![amellnik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amellnik/32/137_2.png) [@amellnik](https://discourse.julialang.org/u/amellnik)\
**Post date:** [April 25, 2018, 7:36pm UTC](https://discourse.julialang.org/t/dataframe-groupby/10544/2 "2018-04-25T19:36:05Z")

</div>

```julia
t = DataFrame(x=[1,1,1,2,2,2], y=[1,1,2,2,1,1], z=[1,2,3,4,5,6])
by(t, [:x, :y], df -> [df[:z]])
# or if you want to set the column name to identity
by(t, [:x, :y]) do df
    td = DataFrame()
    td[:identity] = [df[:z]]
    td
end

```

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [April 26, 2018, 4:38am UTC](https://discourse.julialang.org/t/dataframe-groupby/10544/3 "2018-04-26T04:38:58Z")

</div>

This works as well:

```julia
julia> by(t, [:x, :y], df -> DataFrame(identity = [df[:z]]))
4×3 DataFrames.DataFrame
│ Row │ x │ y │ identity │
├─────┼───┼───┼──────────┤
│ 1 │ 1 │ 1 │ [1, 2] │
│ 2 │ 1 │ 2 │ [3] │
│ 3 │ 2 │ 2 │ [4] │
│ 4 │ 2 │ 1 │ [5, 6] │

```
