pivot
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:
reducedinto aDataRow, where each group is collapsed into a single representative row;aggregatedinto aDataRow, where each group is transformed into a new row of derived values;groupedinto aPivotGroupBystructure, which combinespivotandgroupByoperations and then reduced or aggregated into aDataFrame.
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:
pivotwith a single key column produces aPivotcontaining one column for each unique key (i.e., key column unique values) with the corresponding group;pivotwith multiple keys combined usingandproduces aPivotwith independentcolumn groupsfor each key column, each having subcolumns with the keys corresponding to their unique values;pivotwith multiple keys ordered usingthenproduces aPivotwith nestedcolumn groups, representing a hierarchical structure of keys combinations from the pivoted columns — i.e., one group per unique key combination.
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
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:truefor multiple pivoted columns or when the Pivot has been grouped;falseotherwise.
The Pivot Columns Selector that defines which columns are used as keys for pivoting and in which order.
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:
reducedinto aDataRow, where each group is collapsed into a single representative row;aggregatedinto aDataRow, where each group is transformed into a new row of derived values;groupedinto aPivotGroupBystructure, which combinespivotandgroupByoperations and then reduced or aggregated into aDataFrame.
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).
pivotwith a single key column produces aPivotcontaining one column for each unique key (i.e., key column unique values) with the corresponding group;pivotwith multiple keys combined usingandproduces aPivotwith independentcolumn groupsfor 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
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:truefor multiple pivoted columns or when the Pivot has been grouped;falseotherwise.
The Column Names that defines which columns are used as keys for pivoting.
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:
Return
A new PivotGroupBy that preserves the original groupBy key columns and pivots the provided columns.
Parameters
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);
The Pivot Columns Selector that defines which columns are pivoted.
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:
Return
A new PivotGroupBy that preserves the original groupBy key columns and pivots the provided columns.
Parameters
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);
The Column names that defines which columns are pivoted.
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
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);
The Pivot Columns Selector that defines which columns are used as keys for pivoting and in which order.
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
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);
The Column Names that defines which columns are used as pivot keys for the operation.
Deprecated
It's recommended to migrate to the String or Extension Properties Access API https://kotlin.github.io/dataframe/apilevels.html