# merge

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

Merges several columns into a single column.

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

Reverse operation: [split](split.html)

```KOTLIN
merge { columns }
    [.notNull()]
    .by(separator) | .by { merger } 
    [.into(column) | .intoList() ]

merger: (DataRow).List<T> -> Any
```

Related operations: [Split / merge columns](splitmerge.html)

See [column selectors](columnselectors.html) for how to select the columns for this operation.

```KOTLIN
// Merge two columns into one column "fullName"
df.merge { name.firstName and name.lastName }.by(" ").into("fullName")
```

[Embedded content](resources/org.jetbrains.kotlinx.dataframe.samples.api.Modify.merge.html)

`merger` accepts a `List` of collected values for every row typed by their common type:

```KOTLIN
df.merge { name.firstName and name.lastName }
    .by { it[0] + " (" + it[1].uppercase() + ")" }
    .into("fullName")
```

[Embedded content](resources/org.jetbrains.kotlinx.dataframe.samples.api.Modify.mergeSameWith.html)

When heterogeneous columns are merged, they may need to be cast to valid types in `merger`:

```KOTLIN
df.merge { name.firstName and age and isHappy }
    .by { "${it[0]} aged ${it[1]} is " + (if (it[2] as Boolean) "" else "not ") + "happy" }
    .into("status")
```

[Embedded content](resources/org.jetbrains.kotlinx.dataframe.samples.api.Modify.mergeDifferentWith.html)

By default, when no `separator` or `merger` is specified, values will be merged into the [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/):

```KOTLIN
df.merge { colsOf<Number>() }.into("data")
```

[Embedded content](resources/org.jetbrains.kotlinx.dataframe.samples.api.Modify.mergeDefault.html)

Merged column values can also be exported to [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/):

```KOTLIN
// Merge data from two columns into List<String>
df.merge { name.firstName and name.lastName }.by(",").intoList()
```

[Embedded content](resources/org.jetbrains.kotlinx.dataframe.samples.api.Modify.mergeIntoList.html)

