Skip to content

Group by

Group a table by column values and aggregate each group. These methods are available on pykx.Table and pykx.KeyedTable objects.

The aggregations sum, prod, min, max, mean, median, count, std, sem, skew, kurt, idxmax, idxmin, any and all can be called on the result of groupby. They are described on the metadata and aggregations page.

groupby

groupby(
    by=None,
    axis=0,
    level=None,
    as_index=True,
    sort=True,
    group_keys=True,
    observed=False,
    dropna=True,
)

Group the table by the values of one or more columns.

Parameters:

Name Type Description Default
by str, int or list

The name of the column, or a list of names, to group by.

None
axis int

Not yet implemented, must be 0.

0
level str, int or list

The name or number of the key column, or a list of them, to group by. A key number can only be used on a KeyedTable. by and level cannot both be given.

None
as_index bool

Return the groups as the key of the result.

True
sort bool

Sort the result by the group keys.

True
group_keys bool

Not yet implemented, must be True.

True
observed bool

Not yet implemented, must be False.

False
dropna bool

Drop the groups where any key is null.

True

Returns:

Type Description
GroupbyTable

A GroupbyTable, on which aggregations such as sum can be called.

Examples:

>>> tab = kx.q('([] k:`a`b`a; v:1 2 3)')
>>> tab.groupby('k').sum()
pykx.KeyedTable(pykx.q('
k| v
-| -
a| 4
b| 2
'))
>>> tab.groupby('k', as_index=False).sum()
pykx.KeyedTable(pykx.q('
idx| k v
---| ---
0  | a 4
1  | b 2
'))