# associateBy

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

The `associateBy` function builds a `Map` from a [DataFrame](dataframe.html) by selecting a key for each row using a [row expression](datarow.html#row-expressions).

The rows themselves (or values derived from them) become the map values.

API reference: [associateBy](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/associate-by.html)

If multiple rows produce the same key, only the last row for that key is kept. This matches the behavior of Kotlin’s standard [kotlin.collections.associateBy](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.sequences/associate-by.html) function.

The keys are in the same order as the rows. A key that occurs in several rows appears at the position of its first row, with the row of its last one.

```KOTLIN
df.associateBy { keySelector }

keySelector: (DataRow) -> Key
```

## Related functions

* [toMap](tomap.html) — converts a [DataFrame](dataframe.html) into a `Map` by using column names as keys and their values as map values.

* [associate](associate.html) — builds a map from key–value pairs produced by transforming each row.

## Example

```KOTLIN
df
```

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

Create a map with names as keys:

```KOTLIN
df.associateBy { "${name.firstName} ${name.lastName}" }
```

Output:

```TEXT
{
  Alice Cooper: { name:{ firstName:Alice, lastName:Cooper }, age:15, city:London, weight:54, isHappy:true },
  Bob Dylan: { name:{ firstName:Bob, lastName:Dylan }, age:45, city:Dubai, weight:87, isHappy:true },
  Charlie Daniels: { name:{ firstName:Charlie, lastName:Daniels }, age:20, city:Moscow, isHappy:false },
  Charlie Chaplin: { name:{ firstName:Charlie, lastName:Chaplin }, age:40, city:Milan, isHappy:true },
  Bob Marley: { name:{ firstName:Bob, lastName:Marley }, age:30, city:Tokyo, weight:68, isHappy:true },
  Alice Wolf: { name:{ firstName:Alice, lastName:Wolf }, age:20, weight:55, isHappy:false },
  Charlie Byrd: { name:{ firstName:Charlie, lastName:Byrd }, age:30, city:Moscow, weight:90, isHappy:true },
  Alice Smith: { name:{ firstName:Alice, lastName:Smith }, age:30, city:Milan, weight:52, isHappy:false },
  Bob Brown: { name:{ firstName:Bob, lastName:Brown }, age:15, city:London, weight:60, isHappy:false },
  Charlie Johnson: { name:{ firstName:Charlie, lastName:Johnson }, age:18, city:Dubai, weight:70, isHappy:false }
}
```

Both rules are visible at once if the city is taken as the key: `London` comes first, because the first person from London is in the first row, but it holds the last one. A row without a city gives a `null` key:

```KOTLIN
df.associateBy { city }
```

Output:

```TEXT
{
  London: { name:{ firstName:Bob, lastName:Brown }, age:15, city:London, weight:60, isHappy:false },
  Dubai: { name:{ firstName:Charlie, lastName:Johnson }, age:18, city:Dubai, weight:70, isHappy:false },
  Moscow: { name:{ firstName:Charlie, lastName:Byrd }, age:30, city:Moscow, weight:90, isHappy:true },
  Milan: { name:{ firstName:Alice, lastName:Smith }, age:30, city:Milan, weight:52, isHappy:false },
  Tokyo: { name:{ firstName:Bob, lastName:Marley }, age:30, city:Tokyo, weight:68, isHappy:true },
  null: { name:{ firstName:Alice, lastName:Wolf }, age:20, weight:55, isHappy:false }
}
```

