weighted_avg_by

The weighted_avg_by method creates a new table containing the weighted average for each group.

Syntax

Parameters

ParameterTypeDescription
wcolstr

The name of the weight column.

by optionalUnion[str, list[str]]

The column(s) by which to group data.

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

Returns

A new table containing the weighted average for each group.

Examples

In this example, weighted_avg_by returns the weighted average of the whole table. Because an average cannot be computed for the string column Letter, this column is dropped before applying weighted_avg_by.

In this example, weighted_avg_by returns the weighted average, as grouped by X.