GroupByDocs
Groups the rows of this DataFrame based on the values in one or more specified cols. Each unique value in a key column — or a unique tuple of values for multiple columns — defines the group consisting of all rows where the column(s) contain that value combination.
Returns a GroupBy — a dataframe-like structure that contains all unique combinations of key values along with the corresponding groups of rows (each represented as a DataFrame) as rows.
A GroupBy can then be:
transformedinto a newGroupBy;reducedinto aDataFrame, where each group is collapsed into a single representative row;aggregatedinto aDataFrame, where each group is transformed into one or more rows of derived values;pivotedinto aPivotGroupBystructure, which combinespivotandgroupByoperations and then reduced or aggregated into aDataFrame.
Grouping keys can also be created inline (i.g. by creating a new column using expr or simply renaming the old one using named):
// Create a new column "newName" based on existing "oldName" values
// and use it as a grouping key:
df.groupBy { expr("newName") { oldName.drop(5) } }Check out Grammar.
This can include column groups and nested columns.
See Selecting Columns.
For more information: See groupBy on the documentation website.
Don't confuse this with group, which groups column into column group.
Types
Provides predefined shortcuts for the most common statistical aggregation operations that can be applied to each group within a GroupBy.
Selecting columns for various DataFrame operations can be done in the following ways: