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 |
|---|---|---|
rows of a | the row, as the receiver and as the argument | |
values of a | the value | |
values of a | the position of the value, starting at | |
key–group pairs of a | the pair, as |
Every example on this page uses the same DataFrame:
Iterate over rows
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:
See row expressions.
Iterate over columns
Iterate over cells
forEach on DataColumn
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.
forEach on GroupBy
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.