add
Returns DataFrame
which contains all columns from original DataFrame
followed by newly added columns. Original DataFrame
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.
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 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
age gt 18 into "is adult"
"details" {
name.lastName.length() into "last name length"
"full name" from { name.firstName + " " + name.lastName }
}
}
df.add {
"year of birth" from 2021 - "age"<Int>()
"age"<Int>() gt 18 into "is adult"
"details" {
"name"["lastName"]<String>().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 CityInfo::location
cityInfo.map { it.population } into "population"
}
}
val personWithCityInfo = df.add {
val cityInfo = "city"<String?>().map { queryCityInfo(it) }
"cityInfo" {
cityInfo.map { it.location } into CityInfo::location
cityInfo.map { it.population } into "population"
}
}
Add existing column to DataFrame
val score by columnOf(4, 3, 5, 2, 1, 3, 5)
df.add(score)
df + score
Add all columns from another DataFrame
df.add(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
.
addId(name: String = "id")
Parameters:
20 May 2025