insert

fun <T, C> DataFrame<T>.insert(column: DataColumn<C>): InsertClause<T>

Inserts the given column into this DataFrame.

This function does not immediately insert the new column but instead specifies a column to insert and returns an InsertClause, which serves as an intermediate step. The InsertClause object provides methods to insert a new column using:

  • under - inserts a new column under the specified column group.

  • after - inserts a new column after the specified column.

  • at - inserts a new column at the specified position.

Each method returns a new DataFrame with the inserted column.

Check out Grammar.

This can include column groups and nested columns.

See Selecting Columns.

For more information: See insert on the documentation website.

See also:

Examples:

// Insert a new column "age" under the column group with path ("info", "personal").
df.insert(age).under(pathOf("info", "personal"))

// Insert a new column "count" after the column "url".
df.insert(count).after { url }

Return

An InsertClause for specifying the placement of the new column.

Parameters

column

A single DataColumn to insert into the DataFrame.


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

Creates a new column using the provided expression and inserts it into this DataFrame.

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.

This function does not immediately insert the new column but instead specifies a column to insert and returns an InsertClause, which serves as an intermediate step. The InsertClause object provides methods to insert a new column using:

  • under - inserts a new column under the specified column group.

  • after - inserts a new column after the specified column.

  • at - inserts a new column at the specified position.

Each method returns a new DataFrame with the inserted column.

Check out Grammar.

This can include column groups and nested columns.

See Selecting Columns.

For more information: See insert on the documentation website.

See also:

Examples

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

// Insert a new "fibonacci" column with the Fibonacci sequence under a "math" column group:
// 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.insert("fibonacci") {
if (index() < 2) 1
else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
}.under("math")

Return

An InsertClause for specifying the placement of the newly created column.

Parameters

name

The name of the new column to be created and inserted.

infer

Controls how values are inferred when building the new column. Defaults to Infer.Nulls.

expression

An AddExpression that computes the value for each row of the new column.


inline fun <T, reified R> DataFrame<T>.insert(column: ColumnAccessor<R>, infer: Infer = Infer.Nulls, noinline expression: AddExpression<T, R>): InsertClause<T>
inline fun <T, reified R> DataFrame<T>.insert(column: KProperty<R>, infer: Infer = Infer.Nulls, noinline expression: AddExpression<T, R>): InsertClause<T>

Deprecated

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