Skip to main content
Version: Python

avg_by

avg_by returns the average (mean) of each non-key column for each group. Null values are ignored.

caution

Applying this aggregation to a column where the average can not be computed will result in an error. For example, the average is not defined for a column of string values.

Syntax

table.avg_by(by: Union[str, list[str]]) -> Table

Parameters

ParameterTypeDescription
byUnion[str, list[str]]

The column(s) by which to group data.

  • [] returns total average for all non-key columns (default).
  • ["X"] will output the total average of each group in column X.
  • ["X", "Y"] will output the total average of each group designated from the X and Y columns.

Returns

A new table containing the average for each group.

Examples

In this example, avg_by returns the average value for the table. Because an average can not be computed for the string columns X and Y, these columns are dropped before applying avg_by.

from deephaven import new_table
from deephaven.column import string_col, int_col

source = new_table(
[
string_col("X", ["A", "B", "A", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "N", "O", "N", "P", "M", "O", "P", "M"]),
int_col("Number", [55, 76, 20, 130, 230, 50, 73, 137, 214]),
]
)

result = source.drop_columns(cols=["X", "Y"]).avg_by()

In this example, avg_by returns the average value, as grouped by X. Because an average can not be computed for the string column Y, this column is dropped before applying avg_by.

from deephaven import new_table
from deephaven.column import string_col, int_col

source = new_table(
[
string_col("X", ["A", "B", "A", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "N", "O", "N", "P", "M", "O", "P", "M"]),
int_col("Number", [55, 76, 20, 130, 230, 50, 73, 137, 214]),
]
)

result = source.drop_columns(cols=["Y"]).avg_by(by=["X"])

In this example, avg_by returns the average value, as grouped by X and Y.

from deephaven import new_table
from deephaven.column import string_col, int_col

source = new_table(
[
string_col("X", ["A", "B", "A", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "N", "O", "N", "P", "M", "O", "P", "M"]),
int_col("Number", [55, 76, 20, 130, 230, 50, 73, 137, 214]),
]
)

result = source.avg_by(by=["X", "Y"])