implode

fun <T> DataFrame<T>.implode(dropNA: Boolean = false): DataRow<T>

Merges all values of every column of this DataFrame into a single DataRow.

Each value of the resulting row is a List of all values in the corresponding column. A ColumnGroup is converted into a DataFrame containing its rows.

If dropNA is true, NA values are removed from the merged lists of value columns.

It is the same as df.implode(dropNA) { all() }.single().

Reverse operation: explode.

For more information: See implode on the documentation website.

Example

The examples below use this DataFrame:

nameagecity
Alice15London
Alice15Milan
Bob45Tokyo
Bob45null
// Merges all values of every column into a single row of lists
val row = df.implode()
nameagecity
[Alice, Alice, Bob, Bob][15, 15, 45, 45][London, Milan, Tokyo, null]

Return

A DataRow with all values of every column merged into lists.

Parameters

dropNA

If true, NA values (null, Double.NaN or Float.NaN) are removed from the merged lists. false by default. Has no effect on imploded column groups and frame columns. Values are removed from each imploded column independently, so when several columns are imploded at once, their lists may stop lining up by position, and explode will not restore the original rows.

Type Parameters

T

The schema marker type of this DataFrame.


fun <T, C> DataFrame<T>.implode(dropNA: Boolean = false, columns: ColumnsSelector<T, C>): DataFrame<T>

Merges values in the specified columns into lists, grouped by the values in all other columns — that is, rows with equal values in the other columns are collapsed into a single row.

The other columns are used as grouping keys and keep their types.

If dropNA is true, NA values are removed from the merged lists of value columns.

Imploded columns will change their types:

An imploded ColumnGroup is converted into a FrameColumn.

Reverse operation: explode.

This can include column groups and nested columns.

For more information: See implode on the documentation website.

This implode overload

Select or express columns using the Columns Selection DSL.

This DSL is initiated by a Columns Selector lambda, which operates in the context of the Columns Selection DSL and expects you to return a SingleColumn or ColumnSet (so, a ColumnsResolver). This is an entity formed by calling any (combination) of the functions in the DSL that is or can be resolved into one or more columns.

The Columns Selection DSL allows using Extension Properties for specifying columns type- and name-safe.

Check out: Columns Selection DSL Grammar

    

See Column Selectors on the documentation website.

Examples

The examples below use this DataFrame:

nameagecity
Alice15London
Alice15Milan
Bob45Tokyo
Bob45null
// Merges the "city" values of each person into a list,
// one row per distinct "name" and "age"
df.implode { city }
nameagecity
Alice15[London, Milan]
Bob45[Tokyo, null]
// Same as above, but `null` cities are removed from the lists
df.implode(dropNA = true) { city }
nameagecity
Alice15[London, Milan]
Bob45[Tokyo]

Return

A new DataFrame with one row per distinct combination of values in the other columns, where the specified columns contain merged lists of values.

Parameters

dropNA

If true, NA values (null, Double.NaN or Float.NaN) are removed from the merged lists. false by default. Has no effect on imploded column groups and frame columns. Values are removed from each imploded column independently, so when several columns are imploded at once, their lists may stop lining up by position, and explode will not restore the original rows.

columns

The ColumnsSelector used to select columns to implode.

Type Parameters

T

The schema marker type of this DataFrame.

C

The common type of the selected columns.


fun <T> DataFrame<T>.implode(vararg columns: String, dropNA: Boolean = false): DataFrame<T>

Merges values in the specified columns into lists, grouped by the values in all other columns — that is, rows with equal values in the other columns are collapsed into a single row.

The other columns are used as grouping keys and keep their types.

If dropNA is true, NA values are removed from the merged lists of value columns.

Imploded columns will change their types:

An imploded ColumnGroup is converted into a FrameColumn.

Reverse operation: explode.

This can include column groups and nested columns.

For more information: See implode on the documentation website.

This implode overload

Select single or multiple columns using their names as Strings. (String API).

Examples

The examples below use this DataFrame:

nameagecity
Alice15London
Alice15Milan
Bob45Tokyo
Bob45null
// Merges the "city" values of each person into a list,
// one row per distinct "name" and "age"
df.implode("city")
nameagecity
Alice15[London, Milan]
Bob45[Tokyo, null]
// Same as above, but `null` cities are removed from the lists
df.implode("city", dropNA = true)
nameagecity
Alice15[London, Milan]
Bob45[Tokyo]

Return

A new DataFrame with one row per distinct combination of values in the other columns, where the specified columns contain merged lists of values.

Parameters

columns

The column names used to select columns to implode.

dropNA

If true, NA values (null, Double.NaN or Float.NaN) are removed from the merged lists. false by default. Has no effect on imploded column groups and frame columns. Values are removed from each imploded column independently, so when several columns are imploded at once, their lists may stop lining up by position, and explode will not restore the original rows.

Type Parameters

T

The schema marker type of this DataFrame.


fun <T, C> DataFrame<T>.implode(vararg columns: ColumnReference<C>, dropNA: Boolean = false): DataFrame<T>
fun <T, C> DataFrame<T>.implode(vararg columns: KProperty<C>, dropNA: Boolean = false): DataFrame<T>

Deprecated

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