# Iterating

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

`forEach` calls a function for every row of a [DataFrame](dataframe.html), every value of a [DataColumn](datacolumn.html), or every key–group pair of a [GroupBy](groupby.html). 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](map.html).

API reference: [rows](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/rows.html), [values](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/values.html)

| Operation | Goes over | The function gets |
| --- | --- | --- |
| [forEach](#iterate-over-rows) | rows of a [DataFrame](dataframe.html) | the row, as the receiver and as the argument |
| [forEach](#foreach-on-datacolumn) | values of a [DataColumn](datacolumn.html) | the value |
| [forEachIndexed](#foreach-on-datacolumn) | values of a [DataColumn](datacolumn.html) | the position of the value, starting at `0`, and the value |
| [forEach](#foreach-on-groupby) | key–group pairs of a [GroupBy](groupby.html) | the pair, as `(key, group)` |

Every example on this page uses the same [DataFrame](dataframe.html):

```KOTLIN
df
```

[Embedded content](./resources/iterateDf.html)

## Iterate over rows

```TEXT
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:

Properties:

```KOTLIN
for (row in df) {
    println(row.age)
}

df.forEach {
    println(it.age)
}

df.rows().forEach {
    println(it.age)
}
```

Strings:

```KOTLIN
for (row in df) {
    println(row["age"])
}

df.forEach {
    println(it["age"])
}

df.rows().forEach {
    println(it["age"])
}
```

See [row expressions](datarow.html#row-expressions).

## Iterate over columns

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

## Iterate over cells

```KOTLIN
// 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

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

`forEach` calls the function for every value of a [DataColumn](datacolumn.html), 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](datacolumn.html) of computed values instead, use [map or mapIndexed](map.html#map-on-datacolumn).

```KOTLIN
// Prints the ages, from the first value to the last one:
// 15, 45, 20, 40, 30, 20, 30
df.age.forEach { println(it) }
```

```KOTLIN
// Prints the first names, numbered:
// "1. Alice", "2. Bob", ...
df.name.firstName.forEachIndexed { i, firstName ->
    println("${i + 1}. $firstName")
}
```

## forEach on GroupBy

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

`forEach` calls the function for every key–group pair of a [GroupBy](groupby.html), in the order in which the pairs appear in it. The function gets each pair as a `GroupBy.Entry`: `key` is a [DataRow](datarow.html) with the key values, and `group` is a [DataFrame](dataframe.html) 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`.

Properties:

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

Strings:

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

