[//]: # (title: add) Returns [`DataFrame`](DataFrame.md) which contains all columns from the original [`DataFrame`](DataFrame.md) followed by newly added columns. Original [`DataFrame`](DataFrame.md) is not modified. `add` appends columns to the end of the dataframe by default. If you want to add a single column to a specific position in the dataframe, use [insert](insert.md). **Related operations**: [](addRemove.md) ## Create a new column and add it to [`DataFrame`](DataFrame.md) ```text add(columnName: String) { rowExpression } rowExpression: DataRow.(DataRow) -> Value ``` ```kotlin df.add("year of birth") { 2021 - age } ``` ```kotlin df.add("year of birth") { 2021 - "age"() } ``` See [row expressions](DataRow.md#row-expressions) You can use the `newValue()` function to access value that was already calculated for the preceding row. It is helpful for recurrent computations: ```kotlin df.add("fibonacci") { if (index() < 2) 1 else prev()!!.newValue() + prev()!!.prev()!!.newValue() } ``` ## Create and add several columns to [`DataFrame`](DataFrame.md) ```kotlin add { columnMapping columnMapping ... } columnMapping = column into columnName | columnName from column | columnName from { rowExpression } | columnGroupName { columnMapping columnMapping ... } ``` ```kotlin df.add { "year of birth" from { 2021 - age } expr { age > 18 } into "is adult" "details" { name.lastName.map { it.length } into "last name length" "full name" from { name.firstName + " " + name.lastName } } } ``` ```kotlin df.add { "year of birth" from { 2021 - "age"() } expr { "age"() > 18 } into "is adult" "details" { "name"["lastName"]().map { it.length } into "last name length" "full name" from { "name"["firstName"]() + " " + "name"["lastName"]() } } } ``` ### Create columns using intermediate result Consider this API: ```kotlin class CityInfo(val city: String?, val population: Int, val location: String) fun queryCityInfo(city: String?): CityInfo = CityInfo(city, city?.length ?: 0, "35.5 32.2") ``` Use the following approach to add multiple columns by calling the given API only once per row: ```kotlin val personWithCityInfo = df.add { val cityInfo = city.map { queryCityInfo(it) } "cityInfo" { cityInfo.map { it.location } into "location" cityInfo.map { it.population } into "population" } } ``` ```kotlin val personWithCityInfo = df.add { val cityInfo = "city"().map { queryCityInfo(it) } "cityInfo" { cityInfo.map { it.location } into "location" cityInfo.map { it.population } into "population" } } ``` ## Add existing column to [`DataFrame`](DataFrame.md) ```kotlin val score by columnOf(4, 3, 5, 2, 1, 3, 5) df.addAll(score) df + score ``` ## Add all columns from another [`DataFrame`](DataFrame.md) ```kotlin df.addAll(df1, df2) ``` ## addId Adds a column with sequential values 0, 1, 2,... The new column will be added in the beginning of the column list and will become the first column in [`DataFrame`](DataFrame.md). ``` addId(name: String = "id") ``` **Parameters:** * `name: String = "id"` - name of the new column.