JuliaData / DataFrames.jl

In-memory tabular data in Julia
https://dataframes.juliadata.org/stable/
Other
1.72k stars 367 forks source link

docs for aggregation over grouped array-like elements #3425

Open huangyxi opened 8 months ago

huangyxi commented 8 months ago

Demonstrate how to aggregate over grouped array-like elements. (#3424)

julia> df = DataFrame(a=[1, 1, 2, 2],
                      b=[[1, 2], [2, 3], [3, 4], [4, 5]])
4×2 DataFrame
 Row │ a      b
     │ Int64  Array…
─────┼───────────────
   1 │     1  [1, 2]
   2 │     1  [2, 3]
   3 │     2  [3, 4]
   4 │     2  [4, 5]

julia> gd = groupby(df, :a)
GroupedDataFrame with 2 groups based on key: a
First Group (2 rows): a = 1
 Row │ a      b
     │ Int64  Array…
─────┼───────────────
   1 │     1  [1, 2]
   2 │     1  [2, 3]
⋮
Last Group (2 rows): a = 2
 Row │ a      b
     │ Int64  Array…
─────┼───────────────
   1 │     2  [3, 4]
   2 │     2  [4, 5]

julia> combine(gd, :b => Ref∘sum)
2×2 DataFrame
 Row │ a      b_Ref_sum
     │ Int64  Array…
─────┼──────────────────
   1 │     1  [3, 5]
   2 │     2  [7, 9]