# replace

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

Replaces one or several columns with new columns.

API reference: [replace](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/replace.html)

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

columnExpression: DataFrame.(DataColumn) -> DataColumn
```

Related operations: [Insert / replace columns](insertreplace.html)

See [column selectors](columnselectors.html) for how to select the columns for this operation.

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

[Embedded content](resources/org.jetbrains.kotlinx.dataframe.samples.api.Modify.replace.html)

> **Tip:**
> `replace { columns }.with { columnExpression }` is equivalent to `convert { columns }.to { columnExpression }`. See [convert](convert.html) for details.

## Advanced example

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

Let's create a [DataFrame](dataframe.html) with column `contributors` pointing to JSON resources

```KOTLIN
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](dataframe.html) for every row in `contributors`, effectively converting it into [FrameColumn](datacolumn.html#framecolumn).

The resulting [FrameColumn](datacolumn.html#framecolumn) can be used to create a [GroupBy DataFrame](groupby.html#transformation) and compute [summary statistics](summarystatistics.html) or perform [aggregation](groupby.html#aggregation).

```KOTLIN
val contributors by column<URL>()

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

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

