Reducers

Usage

Reducers are used in reduce to compute the aggregated results obtained by a groupby:

my_table.groupby(table.columnA).reduce(aggregated_result=pw.reducers.my_reducer(my_table.columnB))

We use the following table t in the examples:

colA colB 1 valA -1 2 valA 1 3 valA 2

List of Available Reducers

min

Returns the minimum of the aggregated values, and throws an error if the table is empty.

t.groupby(t.colA).reduce(min=pw.reducers.min(t.colB))
| min ^ENHSR8M... | -1

max

Returns the maximum of the aggregated values, and throws an error if the table is empty.

t.groupby(t.colA).reduce(max=pw.reducers.max(t.colB))
| max ^ENHSR8M... | 2

sum

Returns the sum of the aggregated values.

t.groupby(t.colA).reduce(sum=pw.reducers.sum(t.colB))
| sum ^ENHSR8M... | 2

avg

Returns the average of the aggregated values.

t.groupby(t.colA).reduce(sum=pw.reducers.avg(t.colB))
| sum ^ENHSR8M... | 0.6666666666666666

npsum

Return the sum of the values of aggregated numpy arrays.

t.groupby(t.colA).reduce(sum=pw.reducers.npsum(t.colB))
| sum ^ENHSR8M... | 2

sorted_tuple

Return a sorted tuple containing all the aggregated values.

t.groupby(t.colA).reduce(tuples=pw.reducers.sorted_tuple(t.colB))
| tuples ^ENHSR8M... | (-1, 1, 2)

count

Returning the number of aggregated elements.

t.groupby(t.colA).reduce(count=pw.reducers.count())
| count ^ENHSR8M... | 3

argmin

Returns the index of the minimum aggregated value, returns None if the table is empty.

t.groupby(t.colA).reduce(argmin=pw.reducers.argmin(t.colB))
| argmin ^ENHSR8M... | ^YYY4HAB...

argmax

Returns the index of the maximum aggregated value, returns None if the table is empty.

t.groupby(t.colA).reduce(argmax=pw.reducers.argmax(t.colB))
| argmax ^ENHSR8M... | ^3CZ78B4...