count_distinct
agg.count_distinct returns an aggregator that computes the number of distinct values, within an aggregation group, for each input column.
Syntax
Parameters
| Parameter | Type | Description |
|---|---|---|
| cols | Union[str, list[str]] | The source column(s) for the calculations.
|
| count_nulls optional | bool | Whether or not to count null values. Default is |
Caution
If an aggregation does not rename the resulting column, the aggregation column will appear in the output table, not the input column. If multiple aggregations on the same column do not rename the resulting columns, an error will result, because the aggregations are trying to create multiple columns with the same name. For example, in table.agg_by([agg.sum_(cols=[“X”]), agg.avg(cols=["X"]), both the sum and the average aggregators produce column X, which results in an error.
Returns
An aggregator that computes the number of distinct values, within an aggregation group, for each input column.
Examples
In this example, agg.count_distinct returns the number of distinct values of Y as grouped by X
In this example, agg.count_distinct returns the number of distinct values of Y (renamed to Z), as grouped by X.
In this example, agg.count_distinct returns the number of distinct values of Y and the number of distinct values of Number, as grouped by X.
In this example, agg.count_distinct returns the number of distinct values of Number, as grouped by X and Y.
In this example, agg.count_distinct returns the number of distinct values of Number, and agg.last returns the last Number integer, as grouped by X.
This example demonstrates the effect of the count_nulls parameter.