LazyGroupBy
class LazyGroupBy(lgb, parent, by)
Categories: aggregation
Pending group-by over a TableFrame, produced by group_by.
Obtain one from TableFrame.group_by(...), then call .agg(...) with
reducing expressions such as col("v").sum(). The reduction shortcut
methods (sum / min / max / mean / median /
n_unique) reduce EVERY column and can fail on a table's hidden system
columns; prefer agg(col(...).<reduction>()). count and len are
always safe.
Examples
tf.group_by("g").agg(col("a").mean().alias("average"))
Wrap a grouper; created by TableFrame.group_by, not directly.
Parameters
lgbparentbyMethods
aggdef agg(
*aggs: IntoExpr | Iterable[IntoExpr] = (),
**named_aggs: IntoExpr = {},
) -> TableFrame
Reduce each group to one row using the given expressions.
Each expression is a column reduction -- typically col(name) plus a
reducer and an alias. The result has one row per group: the group
key column(s) followed by the aggregated columns.
Parameters:
*aggsIntoExpr | Iterable[IntoExpr] (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None | Iterable[int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None])Aggregation expressions to compute per group.
**named_aggsIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Aggregation expressions as keyword arguments, where the keyword names the output column.
Example:
tf.group_by("k").agg(col("v").sum().alias("s"))
tf.group_by("k").agg(col("v").mean().alias("m"))
lendef len(name: str | None = None) -> TableFrame
Count the rows in each group.
Safe on any TableFrame (it counts rows, not column values).
Parameters:
Example:
tf.group_by("g").len()
Name the output column to keep it distinct from the data:
tf.group_by("g").len("rows")
countdef count() -> TableFrame
Count the non-null values of each column within each group.
Yields one count column per non-key column. Safe on any
TableFrame (it counts values, not reduces them).
Example:
tf.group_by("g").count()
maxdef max() -> TableFrame
Take the maximum of each non-key column within each group.
Reduces EVERY column; may fail on a table's hidden system columns
through storage. Prefer agg(col(...).max()).
Example:
tf.group_by("g").agg(col("a").max().alias("largest"))
meandef mean() -> TableFrame
Average each non-key column within each group.
Reduces EVERY column; may fail on a table's hidden system columns
through storage. Prefer agg(col(...).mean()).
Example:
tf.group_by("g").agg(col("a").mean().alias("average"))
mediandef median() -> TableFrame
Take the median of each non-key column within each group.
Reduces EVERY column; may fail on a table's hidden system columns
through storage. Prefer agg(col(...).median()).
Example:
tf.group_by("g").agg(col("a").median().alias("middle"))
mindef min() -> TableFrame
Take the minimum of each non-key column within each group.
Reduces EVERY column; may fail on a table's hidden system columns
through storage. Prefer agg(col(...).min()).
Example:
tf.group_by("g").agg(col("a").min().alias("smallest"))
n_uniquedef n_unique() -> TableFrame
Count the distinct values of each non-key column within each group.
Reduces EVERY column; may fail on a table's hidden system columns
through storage. Prefer agg(col(...).n_unique()).
Example:
tf.group_by("g").agg(col("a").n_unique().alias("distinct"))
sumdef sum() -> TableFrame
Sum each non-key column within each group.
Reduces EVERY column; may fail on a table's hidden system columns
through storage. Prefer agg(col(...).sum()).
Example:
tf.group_by("g").agg(col("a").sum().alias("total"))