add
Returns DataFrame which contains all columns from the original DataFrame followed by newly added columns. Original DataFrame is not modified.
API reference: add
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.
Related operations: Add / map / remove columns
Create a new column and add it to DataFrame
add(columnName: String) { rowExpression }
rowExpression: DataRow.(DataRow) -> Value
df.add("year of birth") { 2021 - age }
df.add("year of birth") { 2021 - "age"<Int>() }
See 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:
df.add("fibonacci") {
if (index() < 2) 1
else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
}
Create and add several columns to DataFrame
add {
columnMapping
columnMapping
...
}
columnMapping = column into columnName
| columnName from column
| columnName from { rowExpression }
| columnGroupName {
columnMapping
columnMapping
...
}
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 }
}
}
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>() }
}
}
Consider this API:
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:
val personWithCityInfo = df.add {
val cityInfo = city.map { queryCityInfo(it) }
"cityInfo" {
cityInfo.map { it.location } into "location"
cityInfo.map { it.population } into "population"
}
}
val personWithCityInfo = df.add {
val cityInfo = "city"<String?>().map { queryCityInfo(it) }
"cityInfo" {
cityInfo.map { it.location } into "location"
cityInfo.map { it.population } into "population"
}
}
Add existing column to DataFrame
val score by columnOf(4, 3, 5, 2, 1, 3, 5)
df.addAll(score)
df + score
Add all columns from another DataFrame
df.addAll(df1, df2)
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.
addId(name: String = "id")
Parameters:
02 November 2026