Files
2026-02-08 11:20:43 -10:00

2.2 KiB
Vendored

Replaces one or several columns with new columns.

replace { columns }
    .with(newColumns) | .with { columnExpression }

columnExpression: DataFrame.(DataColumn) -> DataColumn

Related operations:

See column selectors for how to select the columns for this operation.

df.replace { name }.with { name.firstName }
df.replace { colsOf<String?>() }.with { col -> col.map { it?.lowercase() } }
df.replace { age }.with { 2021 - age named "year" }

replace { columns }.with { columnExpression } is equivalent to convert { columns }.to { columnExpression }. See convert for details.

Advanced example

To explore the power of the replace operation, let's consider the following example.

Let's create a DataFrame with column contributors pointing to JSON resources

fun testResource(resourcePath: String): URL = UtilTests::class.java.classLoader.getResource(resourcePath)!!

val interestingRepos = dataFrameOf("name", "url", "contributors")(
    "dataframe", "/dataframe", testResource("dataframeContributors.json"),
    "kotlin", "/kotlin", testResource("kotlinContributors.json"),
)

We can use replace and with to read a DataFrame for every row in contributors, effectively converting it into FrameColumn.

The resulting FrameColumn can be used to create a GroupBy DataFrame and compute summary statistics or perform aggregation.

val contributors by column<URL>()

val df = interestingRepos
    .replace { contributors }
    .with {
        it.mapNotNullValues { url -> DataFrame.readJsonStr(url.readText()) }
    }

df.asGroupBy("contributors").max("contributions")