maxBy

inline fun <T, reified R : Comparable<R & Any>?> DataColumn<T>.maxBy(skipNaN: Boolean = skipNaNDefault, crossinline selector: (T) -> R): T

Returns the first element of this DataColumn for which the given selector returns the maximum value.

The result of the expression is considered the 'input' of this operation.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Throws a NoSuchElementException when there is nothing left to compare, for instance when the input is empty or contains only null (or, if skipNaN is true, only null and NaN) values.

Don't confuse maxBy with maxOf, which returns the maximum selector value itself instead of the element it belongs to.

See also:

  • maxByOrNull — returns null instead of throwing for a column with nothing to compare.

  • minBy — the mirror operation.

  • The Max Operation — an overview of all max modes.

For more information: See maxBy on the documentation website.

Example

// The longest first name in the "name"/"firstName" column
df.name.firstName.maxBy { it.length }

Return

The first element for which selector returns the maximum value.

Parameters

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.

selector

A function that returns the value to compare for each element of this column.

Throws

if there are no values to compare.


inline fun <T, reified C : Comparable<C & Any>?> DataFrame<T>.maxBy(skipNaN: Boolean = skipNaNDefault, crossinline expression: RowExpression<T, C>): DataRow<T>

Returns the first row of this DataFrame for which the given expression returns the maximum value.

The given RowExpression is evaluated for each row of the dataframe. The row is both the receiver and the argument (it) of the expression, so the values in it can be accessed directly.

For more information: See RowExpression on the documentation website.

The result of the expression is considered the 'input' of this operation.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Throws a NoSuchElementException when there is nothing left to compare, for instance when the input is empty or contains only null (or, if skipNaN is true, only null and NaN) values.

Don't confuse maxBy with maxOf, which returns the maximum expression value itself instead of the row it belongs to.

See also:

  • maxByOrNull — returns null instead of throwing when there's nothing to compare.

  • minBy — the mirror operation.

  • sortByDesc — orders all rows instead of taking just the largest one.

  • The Max Operation — an overview of all max modes.

For more information: See maxBy on the documentation website.

Example

// The row with the largest "age"
df.maxBy { age }
// The row with the largest weight-to-age ratio
df.maxBy { (weight ?: 0) / age }

Return

The first DataRow for which expression returns the maximum value.

Parameters

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.

expression

The RowExpression to compute the value to compare for each row.

Throws

if there are no values to compare.


fun <T> DataFrame<T>.maxBy(column: String, skipNaN: Boolean = skipNaNDefault): DataRow<T>

Returns the first row of this DataFrame that has the largest value in the column with the given name.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Throws a NoSuchElementException when there is nothing left to compare, for instance when the input is empty or contains only null (or, if skipNaN is true, only null and NaN) values.

Don't confuse maxBy with maxOf, which returns the maximum value a row expression returns itself, instead of the row it belongs to.

See also:

  • maxByOrNull — returns null instead of throwing when there's nothing to compare.

  • max — returns the largest value itself instead of the row it belongs to.

  • minBy — the mirror operation.

  • sortByDesc — orders all rows instead of taking just the largest one.

  • The Max Operation — an overview of all max modes.

For more information: See maxBy on the documentation website.

Example

// The row with the largest "age"
df.maxBy("age")

Return

The first DataRow with the largest value in the given column.

Parameters

column

The name of the column of this DataFrame to compare the rows by.

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.

Throws

if there are no values to compare.


inline fun <T, G, reified R : Comparable<R & Any>?> GroupBy<T, G>.maxBy(skipNaN: Boolean = skipNaNDefault, crossinline rowExpression: RowExpression<G, R>): ReducedGroupBy<T, G>

Reduces each group of this GroupBy to the first row for which the given rowExpression returns the maximum value.

This operation does not produce a result right away. Instead, it returns a ReducedGroupBy — an intermediate step which can be finished with concat (to get a DataFrame with the selected rows), values, or into.

See GroupBy reducing for more details.

The given RowExpression is evaluated for each row of the dataframe. The row is both the receiver and the argument (it) of the expression, so the values in it can be accessed directly.

For more information: See RowExpression on the documentation website.

The result of the expression is considered the 'input' of this operation.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Groups that have no values to compare cannot select a row, and produce null values instead.

Don't confuse maxBy with maxOf, which returns the maximum value itself instead of the row it belongs to.

See also:

For more information: See maxBy on the documentation website.

Example

// For each city, the full row of the person with the largest "age"
df.groupBy { city }.maxBy { age }.concat()

Return

A ReducedGroupBy with, for each group, the first row for which rowExpression returns the maximum value.

Parameters

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.

rowExpression

The RowExpression to compute the value to compare for each row.


fun <T, G> GroupBy<T, G>.maxBy(column: String, skipNaN: Boolean = skipNaNDefault): ReducedGroupBy<T, G>

Reduces each group of this GroupBy to the first row that has the largest value in the column with the given name.

This operation does not produce a result right away. Instead, it returns a ReducedGroupBy — an intermediate step which can be finished with concat (to get a DataFrame with the selected rows), values, or into.

See GroupBy reducing for more details.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Groups that have no values to compare cannot select a row, and produce null values instead.

Don't confuse maxBy with maxOf, which returns the maximum value a row expression returns itself, instead of the row it belongs to.

See also:

  • max — the maximum value itself instead of the row it belongs to.

  • minBy — the mirror operation.

  • The Max Operation — an overview of all max modes.

For more information: See maxBy on the documentation website.

Example

// For each city, the full row of the person with the largest "age"
df.groupBy { city }.maxBy("age").concat()

Return

A ReducedGroupBy with, for each group, the first row that has the largest value in the given column.

Parameters

column

The name of the column to compare the rows by.

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.


inline fun <T, reified R : Comparable<R & Any>?> Pivot<T>.maxBy(skipNaN: Boolean = skipNaNDefault, crossinline rowExpression: RowExpression<T, R>): ReducedPivot<T>

Reduces this Pivot by taking from each group the first row for which the given rowExpression returns the maximum value.

The given RowExpression is evaluated for each row of the dataframe. The row is both the receiver and the argument (it) of the expression, so the values in it can be accessed directly.

For more information: See RowExpression on the documentation website.

The result of the expression is considered the 'input' of this operation.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Groups that have no values to compare cannot select a row, and produce null values instead.

This operation does not produce a result right away. Instead, it returns a ReducedPivot — an intermediate step which can be finished with values or with.

Don't confuse maxBy with maxOf, which returns the maximum value the expression returns itself, instead of the row.

Check out the Pivot Grammar.

See also:

For more information: See maxBy on the documentation website.

Example

// For each city, the "name" of the person with the largest weight-to-age ratio
df.pivot { city }.maxBy { (weight ?: 0) / age }.with { name }

Return

A ReducedPivot holding, per group, the first row where the rowExpression produced the maximum result.

Parameters

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.

rowExpression

The RowExpression to evaluate for each row.


fun <T> Pivot<T>.maxBy(column: String, skipNaN: Boolean = skipNaNDefault): ReducedPivot<T>

Reduces this Pivot by taking from each group the first row that has the largest value in the given column.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Groups that have no values to compare cannot select a row, and produce null values instead.

This operation does not produce a result right away. Instead, it returns a ReducedPivot — an intermediate step which can be finished with values or with.

Don't confuse maxBy with maxOf, which returns the maximum value a row expression returns itself, instead of the row.

Check out the Pivot Grammar.

See also:

For more information: See maxBy on the documentation website.

Example

// For each city, the "name" of the oldest person
df.pivot { city }.maxBy("age").with { name }

Return

A ReducedPivot holding, per group, the first row with the largest value in the given column.

Parameters

column

The name of the column to compare the rows by.

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.


inline fun <T, reified R : Comparable<R & Any>?> PivotGroupBy<T>.maxBy(skipNaN: Boolean = skipNaNDefault, crossinline rowExpression: RowExpression<T, R>): ReducedPivotGroupBy<T>

Reduces this PivotGroupBy by taking from each group the first row for which the given rowExpression returns the maximum value.

The given RowExpression is evaluated for each row of the dataframe. The row is both the receiver and the argument (it) of the expression, so the values in it can be accessed directly.

For more information: See RowExpression on the documentation website.

The result of the expression is considered the 'input' of this operation.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Groups that have no values to compare cannot select a row, and produce null values instead.

This operation does not produce a result right away. Instead, it returns a ReducedPivotGroupBy — an intermediate step which can be finished with values or with.

Don't confuse maxBy with maxOf, which returns the maximum value the expression returns itself, instead of the row.

Check out the PivotGroupBy Grammar.

See also:

For more information: See maxBy on the documentation website.

Example

// Per city and last name, the "firstName" of the person with the largest weight-to-age ratio
df.pivot { city }.groupBy { name.lastName }.maxBy { (weight ?: 0) / age }.with { name.firstName }

Return

A ReducedPivotGroupBy holding, per group, the first row where the rowExpression produced the maximum result.

Parameters

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.

rowExpression

The RowExpression to evaluate for each row.


fun <T> PivotGroupBy<T>.maxBy(column: String, skipNaN: Boolean = skipNaNDefault): ReducedPivotGroupBy<T>

Reduces this PivotGroupBy by taking from each group the first row that has the largest value in the given column.

Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.

null values in the input are always ignored.

If the input contains NaN values, the result will be NaN, unless skipNaN is set to true.

Groups that have no values to compare cannot select a row, and produce null values instead.

This operation does not produce a result right away. Instead, it returns a ReducedPivotGroupBy — an intermediate step which can be finished with values or with.

Don't confuse maxBy with maxOf, which returns the maximum value a row expression returns itself, instead of the row.

Check out the PivotGroupBy Grammar.

See also:

For more information: See maxBy on the documentation website.

Example

// Per city and last name, the "firstName" of the oldest person
df.pivot { city }.groupBy { name.lastName }.maxBy("age").with { name.firstName }

Return

A ReducedPivotGroupBy holding, per group, the first row with the largest value in the given column.

Parameters

column

The name of the column to compare the rows by.

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.


inline fun <T, reified C : Comparable<C & Any>?> DataFrame<T>.maxBy(column: ColumnReference<C>, skipNaN: Boolean = skipNaNDefault): DataRow<T>
inline fun <T, reified C : Comparable<C & Any>?> DataFrame<T>.maxBy(column: KProperty<C>, skipNaN: Boolean = skipNaNDefault): DataRow<T>
inline fun <T, G, reified C : Comparable<C & Any>?> GroupBy<T, G>.maxBy(column: ColumnReference<C>, skipNaN: Boolean = skipNaNDefault): ReducedGroupBy<T, G>
inline fun <T, G, reified C : Comparable<C & Any>?> GroupBy<T, G>.maxBy(column: KProperty<C>, skipNaN: Boolean = skipNaNDefault): ReducedGroupBy<T, G>
inline fun <T, reified C : Comparable<C & Any>?> Pivot<T>.maxBy(column: ColumnReference<C>, skipNaN: Boolean = skipNaNDefault): ReducedPivot<T>
inline fun <T, reified C : Comparable<C & Any>?> Pivot<T>.maxBy(column: KProperty<C>, skipNaN: Boolean = skipNaNDefault): ReducedPivot<T>
inline fun <T, reified C : Comparable<C & Any>?> PivotGroupBy<T>.maxBy(column: ColumnReference<C>, skipNaN: Boolean = skipNaNDefault): ReducedPivotGroupBy<T>
inline fun <T, reified C : Comparable<C & Any>?> PivotGroupBy<T>.maxBy(column: KProperty<C>, skipNaN: Boolean = skipNaNDefault): ReducedPivotGroupBy<T>

Deprecated

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