# add

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

Returns [DataFrame](dataframe.html) which contains all columns from the original [DataFrame](dataframe.html) followed by newly added columns. Original [DataFrame](dataframe.html) is not modified.

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

`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.html).

Related operations: [Add / map / remove columns](addremove.html)

##  Create a new column and add it to [DataFrame](dataframe.html) 

```TEXT
add(columnName: String) { rowExpression }

rowExpression: DataRow.(DataRow) -> Value
```

Properties:

```KOTLIN
df.add("year of birth") { 2021 - age }
```

Strings:

```KOTLIN
df.add("year of birth") { 2021 - "age"<Int>() }
```

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

See [row expressions](datarow.html#row-expressions)

You can use the `newValue()` function to access the 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<Int>() + prev()!!.prev()!!.newValue<Int>()
}
```

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

##  Create and add several columns to [DataFrame](dataframe.html) 

```KOTLIN
add { 
    columnMapping
    columnMapping
    ...
}

columnMapping = column into columnName 
    | columnName from column 
    | columnName from { rowExpression }
    | columnGroupName { 
        columnMapping
        columnMapping
        ...
    }
```

Properties:

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

Strings:

```KOTLIN
df.add {
    "year of birth" from { 2021 - "age"<Int>() }
    expr { "age"<Int>() > 18 } into "is adult"
    "details" {
        "name"["lastName"]<String>().map { it.length } into "last name length"
        "full name" from { "name"["firstName"]<String>() + " " + "name"["lastName"]<String>() }
    }
}
```

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

### 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:

Properties:

```KOTLIN
val personWithCityInfo = df.add {
    val cityInfo = city.map { queryCityInfo(it) }
    "cityInfo" {
        cityInfo.map { it.location } into "location"
        cityInfo.map { it.population } into "population"
    }
}
```

Strings:

```KOTLIN
val personWithCityInfo = df.add {
    val cityInfo = "city"<String?>().map { queryCityInfo(it) }
    "cityInfo" {
        cityInfo.map { it.location } into "location"
        cityInfo.map { it.population } into "population"
    }
}
```

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

##  Add existing column to [DataFrame](dataframe.html) 

```KOTLIN
val score by columnOf(4, 3, 5, 2, 1, 3, 5)

df.addAll(score)
df + score
```

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

##  Add all columns from another [DataFrame](dataframe.html) 

```KOTLIN
df.addAll(df1, df2)
```

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

## addId

Adds a column with sequential values 0, 1, 2,... The new column will be added at the beginning of the column list and will become the first column in [DataFrame](dataframe.html).

```
addId(name: String = "id")
```

Parameters:

* `name: String = "id"` - name of the new column.

