unique
agg.unique
returns an aggregator that computes:
- the single unique value contained in each specified column,
- a customizable value, if there are no values present,
- or a customizable value, if there is more than one value present.
Syntax
unique(includeNulls: bool=False, noValue: Any=None, nonUniqueValue: Any=None, cols: List[str])
Parameter | Type | Description |
---|---|---|
includeNulls optional | boolean | When set to |
noValue optional | Object | The value to return if there are no values present for the specified columns within a group. The default value is |
nonUniqueValue optional | Object | The value to return if there is more than one distinct value present within a group. The default value is |
cols | List[str] | The source column(s) to compute uniqueness for.
|
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 single unique value, within an aggregation group, for each input column. If there are no unique values or if there are multiple values, the aggregator returns a specified value.
Examples
In the following example, agg.unique
is used to find and return only the unique values of column X
.
from deephaven import new_table
from deephaven.column import string_col
from deephaven import agg as agg
source = new_table([
string_col("X", ["A", "A", "B", "B", "B", "C", "C", "D"]),
string_col("Y", ["Q", "Q", "R", "R", None, "S", "T", None])
])
result = source.agg_by([agg.unique("Y")], by=["X"])
- source
- result
In the following example, agg.unique
is used to find and return only the unique values of column X
. Nulls are included.
from deephaven import new_table
from deephaven.column import string_col
from deephaven import agg as agg
source = new_table([
string_col("X", ["A", "A", "B", "B", "B", "C", "C", "D"]),
string_col("Y", ["Q", "Q", "R", "R", None, "S", "T", None])
])
result = source.agg_by([agg.unique(cols=["Y"])], by=["X"])
- source
- result