DataFrame Groupby

Is there a way to do something similar in DataFrames?

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.

1 Like
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
1 Like

This works as well:

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]   │
1 Like