pivot

fun <T> DataFrame<T>.pivot(inward: Boolean? = null, columns: PivotColumnsSelector<T, *>): Pivot<T>

Splits the rows of this DataFrame and groups them horizontally into new columns based on values from one or several provided columns of the original DataFrame.

Returns a Pivot — a dataframe-like structure that contains all unique combinations of key values as columns (or column groups for multiple keys) with a single row with the corresponding groups for each key combination (each represented as a DataFrame).

Works like DataFrame.groupBy but groups rows horizontally.

A Pivot can then be:

Pivoted columns 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 pivot it:
df.pivot { expr("newName") { oldName.drop(5) } }

Check out Grammar.

This can include column groups and nested columns.

See Selecting Columns.

For more information: See pivot on the documentation website.

This pivot Overload

Select or express pivot columns using the PivotDsl.

PivotDsl defines how key columns are selected and structured in a pivot:

  • pivot with a single key column produces a Pivot containing one column for each unique key (i.e., key column unique values) with the corresponding group;

  • pivot with multiple keys combined using and produces a Pivot with independent column groups for each key column, each having subcolumns with the keys corresponding to their unique values;

  • pivot with multiple keys ordered using then produces a Pivot with nested column groups, representing a hierarchical structure of keys combinations from the pivoted columns — i.e., one group per unique key combination.

See Columns Selection DSL.

Examples

// Pivot by the "city" column
df.pivot { city }

// Independent pivot by "city" and "lastName" (from the "name" column group)
df.pivot { city and name.lastName }

// Hierarchical pivot by two columns with composite ("city", "lastName") keys
df.pivot { city then name.lastName }

Return

A new Pivot containing the unique values of the selected column as new columns (or as column groups for multiple key columns), with their corresponding groups of rows represented as DataFrames.

Parameters

inward

Defines whether the generated columns are nested under a supercolumn:

  • true — pivot key columns are nested under a supercolumn named after the original pivoted column (independently for multiple pivoted columns);

  • false — pivot key columns are not nested (i.e., placed at the top level);

  • null (default) — inferred automatically: true for multiple pivoted columns or when the Pivot has been grouped; false otherwise.

columns

The Pivot Columns Selector that defines which columns are used as keys for pivoting and in which order.


fun <T> DataFrame<T>.pivot(vararg columns: String, inward: Boolean? = null): Pivot<T>

Splits the rows of this DataFrame and groups them horizontally into new columns based on values from one or several provided columns of the original DataFrame.

Returns a Pivot — a dataframe-like structure that contains all unique combinations of key values as columns (or column groups for multiple keys) with a single row with the corresponding groups for each key combination (each represented as a DataFrame).

Works like DataFrame.groupBy but groups rows horizontally.

A Pivot can then be:

Pivoted columns 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 pivot it:
df.pivot { expr("newName") { oldName.drop(5) } }

Check out Grammar.

This can include column groups and nested columns.

See Selecting Columns.

For more information: See pivot on the documentation website.

This pivot Overload

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

  • pivot with a single key column produces a Pivot containing one column for each unique key (i.e., key column unique values) with the corresponding group;

  • pivot with multiple keys combined using and produces a Pivot with independent column groups for each key column, each having subcolumns with the keys corresponding to their unique values;

For pivoting by multiple keys combinations from different columns, use the pivot overload with PivotDsl.

Examples

// Pivot by the "city" column
df.pivot("city")

// Independent pivot by "city" and "lastName"
df.pivot("city", "lastName")

Return

A new Pivot containing the unique values of the selected column as new columns (or as column groups for multiple key columns), with their corresponding groups of rows represented as DataFrames.

Parameters

inward

Defines whether the generated columns are nested under a supercolumn:

  • true — pivot key columns are nested under a supercolumn named after the original pivoted column (independently for multiple pivoted columns);

  • false — pivot key columns are not nested (i.e., placed at the top level);

  • null (default) — inferred automatically: true for multiple pivoted columns or when the Pivot has been grouped; false otherwise.

columns

The Column Names that defines which columns are used as keys for pivoting.


fun <G> GroupBy<*, G>.pivot(inward: Boolean = true, columns: PivotColumnsSelector<G, *>): PivotGroupBy<G>

Pivots the selected columns of this GroupBy groups. 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.

Pivoted columns 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 pivot it:
df.pivot { expr("newName") { oldName.drop(5) } }

This pivot 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:

gb.pivot{ length andage }

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

gb.pivot{ colsOf<Double>() }

Return

A new PivotGroupBy that preserves the original groupBy key columns and pivots the provided columns.

Parameters

inward

Defines whether the generated columns are nested under a supercolumn:

  • true (default) — pivot key columns are nested under a supercolumn named after the original pivoted column (independently for multiple pivoted columns);

  • false — pivot key columns are not nested (i.e., placed at the top level);

columns

The Pivot Columns Selector that defines which columns are pivoted.


fun <G> GroupBy<*, G>.pivot(vararg columns: String, inward: Boolean = true): PivotGroupBy<G>

Pivots the selected columns of this GroupBy groups. 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.

Pivoted columns 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 pivot it:
df.pivot { expr("newName") { oldName.drop(5) } }

This pivot 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:

gb.pivot{ length andage }

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

gb.pivot{ colsOf<Double>() }

Return

A new PivotGroupBy that preserves the original groupBy key columns and pivots the provided columns.

Parameters

inward

Defines whether the generated columns are nested under a supercolumn:

  • true (default) — pivot key columns are nested under a supercolumn named after the original pivoted column (independently for multiple pivoted columns);

  • false — pivot key columns are not nested (i.e., placed at the top level);

columns

The Column names that defines which columns are pivoted.


fun <T> AggregateGroupedDsl<T>.pivot(inward: Boolean = true, columns: PivotColumnsSelector<T, *>): PivotGroupBy<T>

Pivots the selected columns within each group for further pivot aggregations.

This function itself does not directly modify the result of aggregate, but instead creates an intermediate PivotGroupBy. The resulting DataFrame columns produced by its aggregations are then inserted into the final DataFrame returned by aggregate when those aggregation functions are executed (as usual aggregations). Their structure depends on the specific PivotGroupBy aggregations used.

See GroupBy.pivot and PivotGroupByDocs.Aggregation for more information.

For more information: See "pivot inside aggregation" on the documentation website.

Check out PivotGroupBy Grammar.

See also pivotMatches and pivotCounts shortcuts.

This pivot overload

Select or express pivot columns using the PivotDsl.

Example

df.groupBy { name.firstName }.aggregate {
// Pivot the "city" column within each group,
// creating a PivotGroupBy with "firstName" as grouping keys
// and "city" as pivoted columns
pivot { city }.aggregate {
// Aggregate the mean of "age" column values for each
// groupBy × pivot combination group into the "meanAge" column
mean { age } into "meanAge"

// Aggregate the size of each `PivotGroupBy` group
// into the "count" column
count() into "count"
}

// Shortcut for `count` aggregation in "firstName" × "lastName" groups
// into "namesCount" column
pivot { name.lastName }.count() into "namesCount"

// Common `count` aggregation
// into "total" column
count() into "total"
}

Return

A PivotGroupBy for further aggregations.

Parameters

inward

Defines whether the generated columns are nested under a supercolumn:

  • true (default) — pivot key columns are nested under a supercolumn named after the original pivoted column (independently for multiple pivoted columns);

  • false — pivot key columns are not nested (i.e., placed at the top level);

columns

The Pivot Columns Selector that defines which columns are used as keys for pivoting and in which order.


fun <T> AggregateGroupedDsl<T>.pivot(vararg columns: String, inward: Boolean = true): PivotGroupBy<T>

Pivots the selected columns within each group for further pivot aggregations.

This function itself does not directly modify the result of aggregate, but instead creates an intermediate PivotGroupBy. The resulting DataFrame columns produced by its aggregations are then inserted into the final DataFrame returned by aggregate when those aggregation functions are executed (as usual aggregations). Their structure depends on the specific PivotGroupBy aggregations used.

See GroupBy.pivot and PivotGroupByDocs.Aggregation for more information.

For more information: See "pivot inside aggregation" on the documentation website.

Check out PivotGroupBy Grammar.

See also pivotMatches and pivotCounts shortcuts.

This pivot overload

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

Example

df.groupBy("firstName").aggregate {
// Pivot the "city" column within each group,
// creating a PivotGroupBy with "firstName" as grouping keys
// and "city" as pivoted columns
pivot("city").aggregate {
// Aggregate the mean of "age" column values for each
// groupBy × pivot combination group into the "meanAge" column
mean("age") into "meanAge"

// Aggregate the size of each `PivotGroupBy` group
// into the "count" column
count() into "count"
}

// Shortcut for `count` aggregation in "firstName" × "lastName" groups
// into "namesCount" column
pivot("lastName").count() into "namesCount"

// Common `count` aggregation
// into "total" column
count() into "total"
}

Return

A PivotGroupBy for further aggregations.

Parameters

inward

Defines whether the generated columns are nested under a supercolumn:

  • true (default) — pivot key columns are nested under a supercolumn named after the original pivoted column (independently for multiple pivoted columns);

  • false — pivot key columns are not nested (i.e., placed at the top level);

columns

The Column Names that defines which columns are used as pivot keys for the operation.


fun <T> DataFrame<T>.pivot(vararg columns: ColumnReference<*>, inward: Boolean? = null): Pivot<T>
fun <T> DataFrame<T>.pivot(vararg columns: KProperty<*>, inward: Boolean? = null): Pivot<T>
fun <G> GroupBy<*, G>.pivot(vararg columns: ColumnReference<*>, inward: Boolean = true): PivotGroupBy<G>
fun <G> GroupBy<*, G>.pivot(vararg columns: KProperty<*>, inward: Boolean = true): PivotGroupBy<G>
fun <T> AggregateGroupedDsl<T>.pivot(vararg columns: ColumnReference<*>, inward: Boolean = true): PivotGroupBy<T>
fun <T> AggregateGroupedDsl<T>.pivot(vararg columns: KProperty<*>, inward: Boolean = true): PivotGroupBy<T>

Deprecated

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