def group_by(
        self,
        field_or_expr,
        order_by=None,
        reverse=False,
        flat=False,
        match_expr=None,
        sort_expr=None,
        create_index=True,
    ):
        """Creates a view that groups the samples in the collection by a
        specified field or expression.

        Examples::

            import fiftyone as fo
            import fiftyone.zoo as foz
            from fiftyone import ViewField as F

            dataset = foz.load_zoo_dataset("cifar10", split="test")

            #
            # Take 1000 samples at random and group them by ground truth label
            #

            view = dataset.take(1000).group_by("ground_truth.label")

            for group in view.iter_dynamic_groups():
                group_value = group.first().ground_truth.label
                print("%s: %d" % (group_value, len(group)))

            #
            # Variation of above operation that arranges the groups in
            # decreasing order of size and immediately flattens them
            #

            from itertools import groupby

            view = dataset.take(1000).group_by(
                "ground_truth.label",
                flat=True,
                sort_expr=F().length(),
                reverse=True,
            )

            rle = lambda v: [(k, len(list(g))) for k, g in groupby(v)]
            for label, count in rle(view.values("ground_truth.label")):
                print("%s: %d" % (label, count))

        Args:
            field_or_expr: the field or ``embedded.field.name`` to group by, or
                a list of field names defining a compound group key, or a
                :class:`fiftyone.core.expressions.ViewExpression` or
                `MongoDB aggregation expression <https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#aggregation-expressions>`_
                that defines the value to group by
            order_by (None): an optional field by which to order the samples in
                each group
            reverse (False): whether to return the results in descending order.
                Applies both to ``order_by`` and ``sort_expr``
            flat (False): whether to return a grouped collection (False) or a
                flattened collection (True)
            match_expr (None): an optional
                :class:`fiftyone.core.expressions.ViewExpression` or
                `MongoDB aggregation expression <https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#aggregation-expressions>`_
                that defines which groups to include in the output view. If
                provided, this expression will be evaluated on the list of
                samples in each group. Only applicable when ``flat=True``
            sort_expr (None): an optional
                :class:`fiftyone.core.expressions.ViewExpression` or
                `MongoDB aggregation expression <https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#aggregation-expressions>`_
                that defines how to sort the groups in the output view. If
                provided, this expression will be evaluated on the list of
                samples in each group. Only applicable when ``flat=True``
            create_index (True): whether to create an index, if necessary, to
                optimize the grouping. Only applicable when grouping by
                field(s), not expressions

        Returns:
            a :class:`fiftyone.core.view.DatasetView`
        """
        return self._add_view_stage(
            fos.GroupBy(
                field_or_expr,
                order_by=order_by,
                reverse=reverse,
                flat=flat,
                match_expr=match_expr,
                sort_expr=sort_expr,
                create_index=create_index,
            )
        )
Creates a view that groups the samples in the collection by a
        specified field or expression.

        Examples::

            import fiftyone as fo
            import fiftyone.zoo as foz
            from fiftyone import ViewField as F

            dataset = foz.load_zoo_dataset("cifar10", split="test")

            #
            # Take 1000 samples at random and group them by ground truth label
            #

            view = dataset.take(1000).group_by("ground_truth.label")

            for group in view.iter_dynamic_groups():
                group_value = group.first().ground_truth.label
                print("%s: %d" % (group_value, len(group)))

            #
            # Variation of above operation that arranges the groups in
            # decreasing order of size and immediately flattens them
            #

            from itertools import groupby

            view = dataset.take(1000).group_by(
                "ground_truth.label",
                flat=True,
                sort_expr=F().length(),
                reverse=True,
            )

            rle = lambda v: [(k, len(list(g))) for k, g in groupby(v)]
            for label, count in rle(view.values("ground_truth.label")):
                print("%s: %d" % (label, count))

        Args:
            field_or_expr: the field or ``embedded.field.name`` to group by, or
                a list of field names defining a compound group key, or a
                :class:`fiftyone.core.expressions.ViewExpression` or
                `MongoDB aggregation expression <https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#aggregation-expressions>`_
                that defines the value to group by
            order_by (None): an optional field by which to order the samples in
                each group
            reverse (False): whether to return the results in descending order.
                Applies both to ``order_by`` and ``sort_expr``
            flat (False): whether to return a grouped collection (False) or a
                flattened collection (True)
            match_expr (None): an optional
                :class:`fiftyone.core.expressions.ViewExpression` or
                `MongoDB aggregation expression <https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#aggregation-expressions>`_
                that defines which groups to include in the output view. If
                provided, this expression will be evaluated on the list of
                samples in each group. Only applicable when ``flat=True``
            sort_expr (None): an optional
                :class:`fiftyone.core.expressions.ViewExpression` or
                `MongoDB aggregation expression <https://docs.mongodb.com/manual/meta/aggregation-quick-reference/#aggregation-expressions>`_
                that defines how to sort the groups in the output view. If
                provided, this expression will be evaluated on the list of
                samples in each group. Only applicable when ``flat=True``
            create_index (True): whether to create an index, if necessary, to
                optimize the grouping. Only applicable when grouping by
                field(s), not expressions

        Returns:
            a :class:`fiftyone.core.view.DatasetView`
        
fiftyone/core/collections.py