Qu

group_by_agg

Builtin index · Collections & strings

result = group_by_agg(df, "group_col", "value_col", "agg")

Input: df, "group_col", "value_col", "agg" · Output: result

What it is

df is a Table; "group_col"/"value_col" are Str column names; agg is a Str naming the aggregate ("mean" by default, plus "sum", "min", "max", "count", etc.).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = group_by_agg(df, "group_col", "value_col", "agg")
df Tabledf is a Table.
"group_col" Str"group_col"/"value_col" are Str column names.
"value_col" Str"group_col"/"value_col" are Str column names.
"agg" —not documented yet
result Table
Returns a Table: one row per distinct value of group_col, with value_col aggregated.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

df = table(x=[1,2,3,-1,5], y=[10,20,-5,7,2])
pos = filter(df, df.x > 0)
print(pos)

s = sort_by(df, "y")
print(s)

g = table(grp=[1,1,2,2], v=[10,20,30,40])
agg = group_by_agg(g, "grp", "v", "mean")
print(agg)
output
x   y
-  --
1  10
2  20
3  -5
5   2
 x   y
--  --
 3  -5
 5   2
-1   7
 1  10
 2  20
grp  mean_v
    ... 3 more lines

from tour

How it works

group_by_agg(table, "group_col", "value_col", "agg") — the original single-(col,agg) form, unchanged — OR group_by_agg(table, "group_col", [(col1,"agg1"), (col2,"agg2"), ...]) — a single call computing every listed (column, aggregate) pair in one pass over the table (Table::group_by_agg_multi; see its doc comment for why that's genuinely one pass, not the single-spec reduction called once per spec). The spec-list argument is a parenthesized Value::List of 2-element Value::List pairs, matching drop's own "single list argument for several at once" convention ([...] is always a numeric literal in Qu, so a bracketed pair list isn't an option — see drop's doc comment above).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.