Files
df-research/dataframe/docs/StardustDocs/topics/collectionsInterop/associateBy.md
2026-02-08 11:20:43 -10:00

2.3 KiB
Vendored
Raw Blame History

associateBy

Discover `associateBy` operation for Kotlin DataFrame. Discover `associateBy` operation for Kotlin DataFrame. Discover `associateBy` operation for Kotlin DataFrame.

The associateBy function builds a Map from a DataFrame by selecting a key for each row using a row expression.
The rows themselves (or values derived from them) become the map values.

If multiple rows produce the same key, only the last row (or value) for that key is kept. This matches the behavior of Kotlins standard kotlin.collections.associateBy function.

df.associateBy { keySelector }

keySelector: (DataRow) -> Key
  • toMap — converts a DataFrame into a Map by using column names as keys and their values as map values.
  • associate — builds a map from keyvalue pairs produced by transforming each row.

Example

df

Create a map with names as keys:

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

Output:

{
  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 }
}