add

inline fun <reified R, T> DataFrame<T>.add(name: String, infer: Infer = Infer.Nulls, noinline expression: AddExpression<T, R>): DataFrame<T>

Creates a new column using an AddExpression and adds a new column to the end of this DataFrame (at the top level).

With an AddExpression, you define the value that each row in the new column should have. This can be based on values from the same row in the original DataFrame.

You can also use functions like prev and next to access other rows, and combine them with newValue to reference values already computed in the new column. For example, use prev().newValue() to access the new column value from the previous row.

Returns a new DataFrame with the new column appended to the original list of DataFrame.columns.

Example

// Add a new column "sum" that contains the sum of values from the "firstValue"
// and "secondValue" columns for each row.
val dfWithSum = df.add("sum") { firstValue + secondValue }

// Add a "fibonacci" column with the Fibonacci sequence:
// for the first two rows, the value is 1;
// for subsequent rows, it's the sum of the two previous Fibonacci values.
val dfWithFibonacci = df.add("fibonacci") {
if (index() < 2) 1
else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
}

For more information: See add on the documentation website.

Return

new DataFrame with added column.

Parameters

name

name for a new column. If it is empty, a unique column name will be generated. Otherwise, it should be unique for original DataFrame.

infer

a value of Infer that specifies how to compute column type for a new column. Defaults to Infer.Nulls.

expression

AddExpression that computes column value for every DataRow of a new column.

Throws

if DataFrame already contains a column with given name


inline fun <reified R, T> DataFrame<T>.add(path: ColumnPath, infer: Infer = Infer.Nulls, noinline expression: AddExpression<T, R>): DataFrame<T>

Creates a new column using AddExpression and inserts it at the specified ColumnPath.

With an AddExpression, you define the value that each row in the new column should have. This can be based on values from the same row in the original DataFrame.

You can also use functions like prev and next to access other rows, and combine them with newValue to reference values already computed in the new column. For example, use prev().newValue() to access the new column value from the previous row.

For more information: See add on the documentation website.

Returns a new DataFrame with the new column inserted at the given path.

If the specified path is partially or fully missing — that is, if any segment of the path does not correspond to an existing column or column group — all missing parts will be created automatically.

Example

// Add a new column "sum" inside the "info" column group (which will be created if it doesn't exist).
// The column contains the sum of values from the "firstValue" and "secondValue" columns for each row.
val dfWithSum = df.add(pathOf("info", "sum")) { firstValue + secondValue }

Return

A new DataFrame with the added column.

Parameters

path

Target ColumnPath for the new column. If it points to a nested location, intermediate columns will be created if necessary.

infer

A value of Infer that specifies how to compute the column type for the new column. Defaults to Infer.Nulls.

expression

An AddExpression that computes the column value for every DataRow of the new column.

Throws

If the DataFrame already contains a column at the specified path.


fun <T> DataFrame<T>.add(body: AddDsl<T>.() -> Unit): DataFrame<T>

Creates new columns using the AddDsl builder.

An AddDsl allows you to add multiple new columns and column groups to a DataFrame using concise syntax based on from, into operations and AddExpressions.

Returns a new DataFrame with the newly added columns.

Example

val dfWithAdded = df.add {
// Add new column "yearOfBirth" computed as 2021 minus value in "age" column
"yearOfBirth" from { 2021 - age }

// Add column "is adult" with result of age > 18
age > 18 into "is adult"

// Add new column "role" using expression
expr { if ( department == "IT") "developer" else "analyst" } into "role"

// Add column group "details"
group("details") {
// Add column "last name length" with length of lastName
name.lastName.length() into "last name length"

// Add column "full name" by combining firstName and lastName
"full name" from { name.firstName + " " + name.lastName }
}
}

For more information: See add on the documentation website.

Return

A new DataFrame with the added columns.

Parameters

body

An AddDsl expression used to define new columns and column groups.


inline fun <reified R, T, G> GroupBy<T, G>.add(name: String, infer: Infer = Infer.Nulls, noinline expression: AddExpression<G, R>): GroupBy<T, G>

Creates a new column using AddExpression and adds a new column to the end of each group (i.e., DataFrames) of this GroupBy (at the top level).

With an AddExpression, you define the value that each row in the new column should have. This can be based on values from the same row in the original DataFrame.

You can also use functions like prev and next to access other rows, and combine them with newValue to reference values already computed in the new column. For example, use prev().newValue() to access the new column value from the previous row.

Returns a new GroupBy with the new column appended to each group DataFrame to the original list of DataFrame.columns.

Check out GroupBy grammar.

For more information: See "GroupBy Transformation" on the documentation website.

Example

// Add a new column "sum" that contains the sum of values from the "firstValue"
// and "secondValue" columns for each row.
val gbWithSum = gb.add("sum") { firstValue + secondValue }

// Add a "fibonacci" column with the Fibonacci sequence:
// for the first two rows, the value is 1;
// for subsequent rows, it's the sum of the two previous Fibonacci values.
val gbWithFibonacci = gb.add("fibonacci") {
if (index() < 2) 1
else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
}

Return

new GroupBy with added column.

Parameters

name

name for a new column. If it is empty, a unique column name will be generated. Otherwise, it should be unique for original group DataFrames.

infer

a value of Infer that specifies how to compute column type for a new column. Defaults to Infer.Nulls.

expression

AddExpression that computes column value for every DataRow of a new column.

Throws

if group DataFrames already contains a column with given name.


fun <T> DataFrame<T>.add(vararg columns: BaseColumn<*>): DataFrame<T>

Deprecated

Deprecated in favor of `addAll(vararg)` to improve completion. Will be ERROR in 1.1.

Replace with

this.addAll(*columns)

Adds new columns to the end of this DataFrame (at the top level).

Returns a new DataFrame with the new columns appended to the original list of DataFrame.columns.

For more information: See add on the documentation website.

Return

new DataFrame with added columns.

Parameters

columns

columns to add.

Throws

if columns in an expected result have repeated names.

if columns in an expected result have different sizes.


fun <T> DataFrame<T>.add(vararg dataFrames: DataFrame<*>): DataFrame<T>

Deprecated

Deprecated in favor of `addAll(vararg)` to improve completion. Will be ERROR in 1.1.

Replace with

this.addAll(*dataFrames)

Adds all columns from the given dataFrames to the end of this DataFrame (at the top level).

Returns a new DataFrame with the columns from the specified dataFrames appended to the original list of DataFrame.columns.

For more information: See add on the documentation website.

Return

new DataFrame with added columns.

Parameters

dataFrames

dataFrames to get columns from.

Throws

if columns in an expected result have repeated names.

if columns in an expected result have different sizes.


inline fun <reified R, T> DataFrame<T>.add(property: KProperty<R>, infer: Infer = Infer.Nulls, noinline expression: AddExpression<T, R>): DataFrame<T>
inline fun <reified R, T> DataFrame<T>.add(column: ColumnAccessor<R>, infer: Infer = Infer.Nulls, noinline expression: AddExpression<T, R>): DataFrame<T>
inline fun <reified R, T, G> GroupBy<T, G>.add(column: ColumnAccessor<G>, infer: Infer = Infer.Nulls, noinline expression: AddExpression<G, R>): GroupBy<T, G>

Deprecated

It's recommended to migrate to the String or Extension Properties Access API https://kotlin.github.io/dataframe/apilevels.html