DataFrame 1.0 Help

associate

The associate function builds a Map from key–value Pairs produced by applying a transformation to each row of this DataFrame using a row expression.

If multiple rows produce the same key, only the last value for that key is kept. This matches the behavior of Kotlin’s standard kotlin.collections.associate 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 value of its last one.

df.associate { pairSelector } pairSelector: (DataRow) -> Pair
  • toMap — converts a DataFrame into a Map by using column names as keys and their values as map values.

  • associateBy — creates a map with rows as values.

Example

df

Create a map from name to age using a pair selector:

df.associate { "${name.firstName} ${name.lastName}" to age }

Output:

{ Alice Cooper: 15, Bob Dylan: 45, Charlie Daniels: 20, Charlie Chaplin: 40, Bob Marley: 30, Alice Wolf: 20, Charlie Byrd: 30, Alice Smith: 30, Bob Brown: 15, Charlie Johnson: 18 }

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:

df.associate { city to "${name.firstName} ${name.lastName}" }

Output:

{ London: Bob Brown, Dubai: Charlie Johnson, Moscow: Charlie Byrd, Milan: Alice Smith, Tokyo: Bob Marley, null: Alice Wolf }
29 September 2026