Groupby

class nvtabular.ops.Groupby(groupby_cols=None, sort_cols=None, aggs='list', name_sep='_', ascending=True)[source]

Bases: nvtabular.ops.operator.Operator

Groupby Transformation

Locally transform each partition of a Dataset with one or more groupby aggregations.

WARNING: This transformation does NOT move data between partitions. Please make sure that the target Dataset object is already shuffled by groupby_cols, otherwise the output may be incorrect. See: Dataset.shuffle_by_keys.

Example usage:

groupby_features = [
    'user_id', 'session_id', 'month', 'prod_id',
] >> ops.Groupby(
    groupby_cols=['user_id', 'session_id'],
    sort_cols=['month'],
    aggs={
        'prod_id': 'list',
        'month': ['first', 'last'],
    },
)
processor = nvtabular.Workflow(groupby_features)
Parameters
  • groupby_cols (str or list of str) – The column names to be used as groupby keys.

  • sort_cols (str or list of str) – Columns to be used to sort each partition before groupby aggregation is performed. If this argument is not specified, the results will not be sorted.

  • aggs (dict, list or str) – Groupby aggregations to perform. Supported list-based aggregations include “list”, “first” & “last”. Most conventional aggregations supported by Pandas/cuDF are also allowed (e.g. “sum”, “count”, “max”, “mean”, etc.).

  • name_sep (str) – String separator to use for new column names.

transform(col_selector: merlin.dag.selector.ColumnSelector, df: pandas.core.frame.DataFrame)pandas.core.frame.DataFrame[source]

Transform the dataframe by applying this operator to the set of input columns

Parameters
  • columns (list of str or list of list of str) – The columns to apply this operator to

  • df (Dataframe) – A pandas or cudf dataframe that this operator will work on

Returns

Returns a transformed dataframe for this operator

Return type

DataFrame

compute_output_schema(input_schema: merlin.schema.schema.Schema, col_selector: merlin.dag.selector.ColumnSelector, prev_output_schema: Optional[merlin.schema.schema.Schema] = None)merlin.schema.schema.Schema[source]
column_mapping(col_selector)[source]