groupBy

fun <T> DataFrame<T>.groupBy(moveToTop: Boolean = true, cols: ColumnsSelector<T, *>): GroupBy<T, T>

Groups the rows of this DataFrame based on the values in one or more specified key columns. 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:

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.

See also pivot that groups rows of DataFrame vertically.

This groupBy Overload

Select or express columns using the Columns Selection DSL.

This DSL is initiated by a Columns Selector lambda, which operates in the context of the Columns Selection DSL and expects you to return a SingleColumn or ColumnSet (so, a ColumnsResolver). This is an entity formed by calling any (combination) of the functions in the DSL that is or can be resolved into one or more columns.

The Columns Selection DSL allows using Extension Properties for specifying columns type- and name-safe.

Check out: Columns Selection DSL Grammar

    

See Column Selectors on the documentation website.

For example:

df.groupBy{ length andage }

df.groupBy{ cols(1..5) }

df.groupBy{ colsOf<Double>() }

Return

A new GroupBy containing the unique combinations of values from the provided key columns, together with their corresponding groups of rows.

Parameters

moveToTop

Specifies whether nested grouping columns should be moved to the top level or kept inside a ColumnGroup. Defaults to true.

cols

The Columns Selector that defines which columns are used as keys for grouping.


fun <T> DataFrame<T>.groupBy(vararg cols: String): GroupBy<T, T>

Groups the rows of this DataFrame based on the values in one or more specified key columns. 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:

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.

See also pivot that groups rows of DataFrame vertically.

This groupBy Overload

Select single or multiple columns using their names as Strings. (String API).

For example:

df.groupBy("length", "age")

Return

A new GroupBy containing the unique combinations of values from the provided key columns, together with their corresponding groups of rows.

Parameters

cols

The Column names that defines which columns are used as keys for grouping.


fun <T> Pivot<T>.groupBy(moveToTop: Boolean = true, columns: ColumnsSelector<T, *>): PivotGroupBy<T>

Groups the rows of this Pivot groups based on the values in one or more specified key columns. Returns a PivotGroupBy.

PivotGroupBy is a dataframe-like structure that combines Pivot and GroupBy, representing a matrix table with vertical Pivot groups (as columns) and horizontal GroupBy groups (as rows), where each cell represents a group corresponding to both the GroupBy and Pivot key.

Reversed order of pivot and groupBy (i.e., DataFrame.pivot + Pivot.groupBy or DataFrame.groupBy + GroupBy.pivot) will produce the same result.

PivotGroupBy can be reduced or aggregated into a DataFrame.

Check out PivotGroupBy Grammar.

For more information: See "pivot + groupBy" on the documentation website.

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) } }

This groupBy Overload

Select or express columns using the Columns Selection DSL.

This DSL is initiated by a Columns Selector lambda, which operates in the context of the Columns Selection DSL and expects you to return a SingleColumn or ColumnSet (so, a ColumnsResolver). This is an entity formed by calling any (combination) of the functions in the DSL that is or can be resolved into one or more columns.

The Columns Selection DSL allows using Extension Properties for specifying columns type- and name-safe.

Check out: Columns Selection DSL Grammar

    

See Column Selectors on the documentation website.

For example:

pivot.groupBy{ length andage }

pivot.groupBy{ cols(1..5) }

pivot.groupBy{ colsOf<Double>() }

Return

A new PivotGroupBy that preserves the original pivot key columns and uses the provided columns as groupBy keys.

Parameters

moveToTop

Specifies whether nested grouping columns should be moved to the top level or kept inside a ColumnGroup. Defaults to true.

columns

The Columns Selector that defines which columns are used as keys for grouping.


fun <T> Pivot<T>.groupBy(vararg columns: String): PivotGroupBy<T>

Groups the rows of this Pivot groups based on the values in one or more specified key columns. Returns a PivotGroupBy.

PivotGroupBy is a dataframe-like structure that combines Pivot and GroupBy, representing a matrix table with vertical Pivot groups (as columns) and horizontal GroupBy groups (as rows), where each cell represents a group corresponding to both the GroupBy and Pivot key.

Reversed order of pivot and groupBy (i.e., DataFrame.pivot + Pivot.groupBy or DataFrame.groupBy + GroupBy.pivot) will produce the same result.

PivotGroupBy can be reduced or aggregated into a DataFrame.

Check out PivotGroupBy Grammar.

For more information: See "pivot + groupBy" on the documentation website.

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) } }

This groupBy Overload

Select single or multiple columns using their names as Strings. (String API).

For example:

df.groupBy("length", "age")

Return

A new PivotGroupBy that preserves the original pivot key columns and uses the provided columns as groupBy keys.

Parameters

columns

The Column names that defines which columns are used as keys for grouping.


fun <T> DataFrame<T>.groupBy(vararg cols: KProperty<*>): GroupBy<T, T>
fun <T> DataFrame<T>.groupBy(vararg cols: ColumnReference<*>, moveToTop: Boolean = true): GroupBy<T, T>
fun <T> Pivot<T>.groupBy(vararg columns: ColumnReference<*>): PivotGroupBy<T>
fun <T> Pivot<T>.groupBy(vararg columns: KProperty<*>): PivotGroupBy<T>

Deprecated

It's recommended to migrate to the String or Extension Properties Access API https://kotlin.github.io/dataframe/apilevels.html