valueCounts

fun <T> DataColumn<T>.valueCounts(sort: Boolean = true, ascending: Boolean = false, dropNA: Boolean = true, resultColumn: String = defaultCountColumnName): DataFrame<ValueCount>

Returns a DataFrame containing counts of unique values in this DataColumn.

The resulting DataFrame contains:

  • the column with the distinct values of the original DataColumn

  • a new Int column with the number of occurrences of each value. This column is called "count" by default unless it is overridden by resultColumn.

By default, the result is sorted by count in descending order (the most frequent value first), and NA values (nulls and NaNs) are not counted.

See also:

For more information: See valueCounts on the documentation website.

All summary statistics: See "Summary statistics" on the documentation website.

Example

// Counts the unique values in the "city" column,
// starting with the most frequent one
df.city.valueCounts()

// Counts the unique values in the "city" column, including `null`s,
// in the order of their first occurrence, in a column named "quantity"
df.city.valueCounts(sort = false, dropNA = false, resultColumn = "quantity")

Return

A DataFrame with the distinct values of this DataColumn and their counts.

Parameters

sort

If true (default), the result is sorted by count. Otherwise, the counted values keep the order of their first occurrence.

ascending

The sorting direction. If false (default), the most frequent values come first. Only used when sort is true.

dropNA

If true (default), NA values (nulls and NaNs) are not counted and are excluded from the result.

resultColumn

The name of the resulting count column. Default — "count". If a column with this name is already present, the name is made unique by appending a number to it (for example, "count" becomes "count1").


fun <T> DataFrame<T>.valueCounts(sort: Boolean = true, ascending: Boolean = false, dropNA: Boolean = true, resultColumn: String = defaultCountColumnName, columns: ColumnsSelector<T, *>? = null): DataFrame<T>

Returns a DataFrame containing the counts of unique rows (or combinations of selected values) in this DataFrame.

Rows are compared by the values in the selected columns. If no columns are selected, values from all columns are used.

This can include column groups and nested columns.

The resulting DataFrame contains:

  • the distinct combinations of these values,

  • a new Int column with the number of occurrences of each combination. This column is called "count" by default unless it is overridden by resultColumn.

By default, the result is sorted by count in descending order (the most frequent combination first), and a row is not counted at all if any of its selected values is NA.

See also:

For more information: See valueCounts on the documentation website.

All summary statistics: See "Summary statistics" on the documentation website.

This valueCounts 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.

Example

// Counts the unique combinations of the values in the "name" and "city" columns,
// starting with the most frequent one
df.valueCounts { name and city }

// Counts the unique rows of the whole dataframe,
// including rows with `NA` values, in the order of their first occurrence
df.valueCounts(sort = false, dropNA = false)

Return

A DataFrame with the distinct value combinations and their counts.

Parameters

sort

If true (default), the result is sorted by count. Otherwise, the counted values keep the order of their first occurrence.

ascending

The sorting direction. If false (default), the most frequent values come first. Only used when sort is true.

dropNA

If true (default), NA values (nulls and NaNs) are not counted and are excluded from the result.

resultColumn

The name of the resulting count column. Default — "count". If a column with this name is already present, the name is made unique by appending a number to it (for example, "count" becomes "count1").

columns

The optional ColumnsSelector that selects the columns whose distinct value combinations are counted. If null or omitted, all columns are used.


fun <T> DataFrame<T>.valueCounts(vararg columns: String, sort: Boolean = true, ascending: Boolean = false, dropNA: Boolean = true, resultColumn: String = defaultCountColumnName): DataFrame<T>

Returns a DataFrame containing the counts of unique rows (or combinations of selected values) in this DataFrame.

Rows are compared by the values in the selected columns. If no columns are selected, values from all columns are used.

This can include column groups and nested columns.

The resulting DataFrame contains:

  • the distinct combinations of these values,

  • a new Int column with the number of occurrences of each combination. This column is called "count" by default unless it is overridden by resultColumn.

By default, the result is sorted by count in descending order (the most frequent combination first), and a row is not counted at all if any of its selected values is NA.

See also:

For more information: See valueCounts on the documentation website.

All summary statistics: See "Summary statistics" on the documentation website.

This valueCounts Overload

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

Example

// Counts the unique combinations of the values in the "name" and "city" columns,
// starting with the most frequent one
df.valueCounts("name", "city")

Return

A DataFrame with the distinct value combinations and their counts.

Parameters

columns

(optional) The names of the columns whose distinct value combinations are counted. If not supplied, all columns are selected.

sort

If true (default), the result is sorted by count. Otherwise, the counted values keep the order of their first occurrence.

ascending

The sorting direction. If false (default), the most frequent values come first. Only used when sort is true.

dropNA

If true (default), NA values (nulls and NaNs) are not counted and are excluded from the result.

resultColumn

The name of the resulting count column. Default — "count". If a column with this name is already present, the name is made unique by appending a number to it (for example, "count" becomes "count1").


fun <T> DataFrame<T>.valueCounts(vararg columns: ColumnReference<*>, sort: Boolean = true, ascending: Boolean = false, dropNA: Boolean = true, resultColumn: String = defaultCountColumnName): DataFrame<T>
fun <T> DataFrame<T>.valueCounts(vararg columns: KProperty<*>, sort: Boolean = true, ascending: Boolean = false, dropNA: Boolean = true, resultColumn: String = defaultCountColumnName): DataFrame<T>

Deprecated

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