pivotCounts

fun <T> DataFrame<T>.pivotCounts(inward: Boolean = true, columns: PivotColumnsSelector<T, *>): DataFrame<T>

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 become column groups corresponding to value combinations when using then, similar to pivot);

  • 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 keys in GroupBy).

  • Cells contain an Int value indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in the DataFrame.

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:

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

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

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 pivot keys for the operation.


fun <T> DataFrame<T>.pivotCounts(vararg columns: String, inward: Boolean = true): DataFrame<T>

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 become column groups corresponding to value combinations when using then, similar to pivot);

  • 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 keys in GroupBy).

  • Cells contain an Int value indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in the DataFrame.

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

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 pivot keys for the operation.


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

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 become column groups corresponding to value combinations when using then, similar to pivot);

  • 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 keys in GroupBy).

  • Cells contain an Int value indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in the DataFrame.

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:

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

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

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 pivot keys for the operation.


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

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 become column groups corresponding to value combinations when using then, similar to pivot);

  • 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 keys in GroupBy).

  • Cells contain an Int value indicating how many rows with the corresponding combination of values (horizontal and vertical) exist in the DataFrame.

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

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> AggregateGroupedDsl<T>.pivotCounts(inward: Boolean = true, columns: PivotColumnsSelector<T, *>): DataFrame<T>

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:

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

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

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>.pivotCounts(vararg columns: String, inward: Boolean = true): DataFrame<T>

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

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 keys for pivoting and in which order.


fun <T> DataFrame<T>.pivotCounts(vararg columns: ColumnReference<*>, inward: Boolean = true): DataFrame<T>
fun <T> DataFrame<T>.pivotCounts(vararg columns: KProperty<*>, inward: Boolean = true): DataFrame<T>
fun <G> GroupBy<*, G>.pivotCounts(vararg columns: ColumnReference<*>, inward: Boolean = true): DataFrame<G>
fun <G> GroupBy<*, G>.pivotCounts(vararg columns: KProperty<*>, inward: Boolean = true): DataFrame<G>
fun <T> AggregateGroupedDsl<T>.pivotCounts(vararg columns: ColumnReference<*>, inward: Boolean = true): DataFrame<T>
fun <T> AggregateGroupedDsl<T>.pivotCounts(vararg columns: KProperty<*>, inward: Boolean = true): DataFrame<T>

Deprecated

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