replace
Replaces one or several columns with new columns.
replace { columns }
.with(newColumns) | .with { columnExpression }
columnExpression: DataFrame.(DataColumn) -> DataColumn
See column selectors
df.replace { name }.with { name.firstName }
df.replace { colsOf<String?>() }.with { it.lowercase() }
df.replace { age }.with { 2021 - age named "year" }
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")
Last modified: 27 September 2024