Skip to main content
Version: Python

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])
ParameterTypeDescription
includeNulls optionalboolean

When set to true, the aggregation treats null values as a countable value. The default value is false.

noValue optionalObject

The value to return if there are no values present for the specified columns within a group. The default value is null.

nonUniqueValue optionalObject

The value to return if there is more than one distinct value present within a group. The default value is null.

colsList[str]

The source column(s) to compute uniqueness for.

  • "X" will output the single unique value in the X column for each group.
  • "Y = X" will output the single unique value in the X column for each group and rename it to Y.
  • "X", "A = B" will output the single unique value in the X column for each group and the single unique value in the B value column renaming it to A.
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"])

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"])