pivotCounts
Computes a count matrix (similar to frequency encoding) for the values in the specified columns of this DataFrame, returning a new DataFrame where:
Columns represent all unique values from the selected
columns(they becomecolumn groupscorresponding to value combinations when usingthen, similar topivot);Rows correspond to all unique combinations of values from the remaining columns; each combination is represented in dedicated key columns that store a distinct set of values for each row (similar to
keysinGroupBy).Cells contain an
Intvalue indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in theDataFrame.
This function combines pivot, groupByOther, and count operations into a single call.
This can include column groups and nested columns.
See Selecting Columns.
For more information: See pivotCounts on the documentation website.
See also: pivotMatches, which performs a similar operation but checks if there is any matching row instead of counting them to produce a Boolean matrix.
This pivotCounts 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 }Example
// Compute number of matching rows for all unique values of "city"
// and "name" (independently) across all possible combinations
// of values in the remaining columns.
df.pivotCounts { city and name }Return
A new DataFrame representing a counting matrix — with grouping key columns as rows, pivot key values as columns, and the number of rows with the corresponding combinations in the cells.
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 pivot keys for the operation.
Computes a count matrix (similar to frequency encoding) for the values in the specified columns of this DataFrame, returning a new DataFrame where:
Columns represent all unique values from the selected
columns(they becomecolumn groupscorresponding to value combinations when usingthen, similar topivot);Rows correspond to all unique combinations of values from the remaining columns; each combination is represented in dedicated key columns that store a distinct set of values for each row (similar to
keysinGroupBy).Cells contain an
Intvalue indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in theDataFrame.
This function combines pivot, groupByOther, and count operations into a single call.
This can include column groups and nested columns.
See Selecting Columns.
For more information: See pivotCounts on the documentation website.
See also: pivotMatches, which performs a similar operation but checks if there is any matching row instead of counting them to produce a Boolean matrix.
This pivotCounts Overload
Select single or multiple columns using their names as Strings. (String API).
Example
// Compute number of matching rows for all unique values of "city"
// and "name" (independently) across all possible combinations
// of values in the remaining columns.
df.pivotCounts("city", "name")Return
A new DataFrame representing a counting matrix — with grouping key columns as rows, pivot key values as columns, and the number of rows with the corresponding combinations in the cells.
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 pivot keys for the operation.
Computes a count matrix (similar to frequency encoding) for the values in the specified columns within each group of this GroupBy, returning a new DataFrame where:
Columns represent all unique values from the selected
columns(they becomecolumn groupscorresponding to value combinations when usingthen, similar topivot);Rows correspond to all unique combinations of values from the grouping columns; each combination is represented in dedicated key columns that store a distinct set of values for each row (similar to
keysinGroupBy).Cells contain an
Intvalue indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in theDataFrame.
This function combines pivot and count operations into a single call.
This can include column groups and nested columns.
See Selecting Columns.
For more information: See pivotCounts on the documentation website.
See also: pivotMatches, which performs a similar operation but checks if there is any matching row instead of counting them.
This pivotCounts 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 }Example
// Compute number of matching rows for all unique values of "city"
// and "name" (independently) across all grouping key combinations.
df.pivotCounts { city and name }Return
A new DataFrame representing a counting matrix — with grouping key columns as rows, pivot key values as columns, and the number of rows with the corresponding combinations in the cells.
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 pivot keys for the operation.
Computes a count matrix (similar to frequency encoding) for the values in the specified columns within each group of this GroupBy, returning a new DataFrame where:
Columns represent all unique values from the selected
columns(they becomecolumn groupscorresponding to value combinations when usingthen, similar topivot);Rows correspond to all unique combinations of values from the grouping columns; each combination is represented in dedicated key columns that store a distinct set of values for each row (similar to
keysinGroupBy).Cells contain an
Intvalue indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in theDataFrame.
This function combines pivot and count operations into a single call.
This can include column groups and nested columns.
See Selecting Columns.
For more information: See pivotCounts on the documentation website.
See also: pivotMatches, which performs a similar operation but checks if there is any matching row instead of counting them.
This pivotCounts Overload
Select single or multiple columns using their names as Strings. (String API).
Example
// Compute number of matching rows for all unique values of "city"
// and "name" (independently) across all grouping key combinations.
df.pivotCounts("city", "name")Return
A new DataFrame representing a counting matrix — with grouping key columns as rows, pivot key values as columns, and the number of rows with the corresponding combinations in the cells.
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.
Computes the pivotCounts statistic for the selected columns within each group and adds it to the aggregate result.
This is a shortcut for combining pivot and count.
The resulting DataFrame columns are inserted into the final DataFrame returned by aggregate. The resulting column name can be specified using into.
See GroupBy.pivotCounts for more details.
For more information: See pivotCounts on the documentation website.
See also: pivot, pivotMatches.
This pivotCounts 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 }Example
df.groupBy { name.firstName }.aggregate {
// Compute number of for all unique values of "city"
// across all "name.firstName" key values and adds it to the aggregation result
pivotCounts { city }
}Return
A new DataFrame representing a counting matrix — with grouping key columns as rows, pivot key values as columns, and the number of rows with the corresponding combinations in the cells. This DataFrame is added to the aggregate result.
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.
Computes the pivotCounts statistic for the selected columns within each group and adds it to the aggregate result.
This is a shortcut for combining pivot and count.
The resulting DataFrame columns are inserted into the final DataFrame returned by aggregate. The resulting column name can be specified using into.
See GroupBy.pivotCounts for more details.
For more information: See pivotCounts on the documentation website.
See also: pivot, pivotMatches.
This pivotCounts overload
Select single or multiple columns using their names as Strings. (String API).
Example
df.groupBy("firstName").aggregate {
// Compute number of for all unique values of "city"
// across all "firstName" key values and adds it to the aggregation result
pivotCounts("city")
}Return
A new DataFrame representing a counting matrix — with grouping key columns as rows, pivot key values as columns, and the number of rows with the corresponding combinations in the cells. This DataFrame is added to the aggregate result.
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 keys for pivoting and in which order.
Deprecated
It's recommended to migrate to the String or Extension Properties Access API https://kotlin.github.io/dataframe/apilevels.html