AxisKeys.jl icon indicating copy to clipboard operation
AxisKeys.jl copied to clipboard

Feature request: aggregation function for wrapdims (and populate!)

Open fredcallaway opened this issue 3 years ago • 0 comments

This is basically what pandas.pivot_table does. Here's a naive implementation. I imagine this could be done more efficiently though.

julia> function wrap_reduce(df, aggregator, val, keys...)
    grp = groupby(df, collect(keys))
    agg = combine(grp, val => aggregator => :_val)
    wrapdims(agg, :_val, keys...)
end

julia> df = DataFrame([
    1 1
    1 1
    1 2
    2 3
    2 5
], ["x", "y"])

julia> wrap_reduce(df, sum, :y, :x)
1-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   x ∈ 2-element Vector{Int64}
And data, 2-element Vector{Int64}:
 (1)  4
 (2)  8

fredcallaway avatar Mar 22 '22 20:03 fredcallaway