DataFrame 1.0 Help

Iterating

forEach calls a function for every row of a DataFrame, every value of a DataColumn, or every key–group pair of a GroupBy. It returns Unit, so it is only useful for its side effects, such as printing values or collecting them. To get a result instead, use map.

Operation

Goes over

The function gets

forEach

rows of a DataFrame

the row, as the receiver and as the argument

forEach

values of a DataColumn

the value

forEachIndexed

values of a DataColumn

the position of the value, starting at 0, and the value

forEach

key–group pairs of a GroupBy

the pair, as (key, group)

Every example on this page uses the same DataFrame:

df

Iterate over rows

forEach { row: DataRow -> Unit }

The function is called for every row, from the first one to the last one. It gets the row both as its receiver and as its argument, so inside it age and it.age mean the same thing. A for loop and rows() go over the same rows in the same order:

for (row in df) { println(row.age) } df.forEach { println(it.age) } df.rows().forEach { println(it.age) }
for (row in df) { println(row["age"]) } df.forEach { println(it["age"]) } df.rows().forEach { println(it["age"]) }

See row expressions.

Iterate over columns

df.columns().forEach { println(it.name()) }

Iterate over cells

// from top to bottom, then from left to right df.values().forEach { println(it) } // from left to right, then from top to bottom df.values(byRows = true).forEach { println(it) }

forEach on DataColumn

forEach { value: T -> Unit } forEachIndexed { index: Int, value: T -> Unit }

forEach calls the function for every value of a DataColumn, from the first one to the last one. forEachIndexed does the same and also gives the position of every value; the position of the first value is 0. To get a new DataColumn of computed values instead, use map or mapIndexed.

// Prints the ages, from the first value to the last one: // 15, 45, 20, 40, 30, 20, 30 df.age.forEach { println(it) }
// Prints the first names, numbered: // "1. Alice", "2. Bob", ... df.name.firstName.forEachIndexed { i, firstName -> println("${i + 1}. $firstName") }

forEach on GroupBy

forEach { (key: DataRow, group: DataFrame) -> Unit }

forEach calls the function for every key–group pair of a GroupBy, in the order in which the pairs appear in it. The function gets each pair as a GroupBy.Entry: key is a DataRow with the key values, and group is a DataFrame with the rows of that group. The pair is the argument of the function, so destructure it as (key, group) or use it.key and it.group.

// Prints the number of people per city: // "London: 1", "Dubai: 1", "Moscow: 2", ... df.groupBy { city }.forEach { (key, group) -> println("${key.city}: ${group.rowsCount()}") }
// Prints the number of people per city: // "London: 1", "Dubai: 1", "Moscow: 2", ... df.groupBy("city").forEach { (key, group) -> println("${key["city"]}: ${group.rowsCount()}") }
15 October 2026