# Apply a function in-place to a DataFrame column and return a new dataframe with replaced column

**URL:** <https://discourse.julialang.org/t/apply-a-function-in-place-to-a-dataframe-column-and-return-a-new-dataframe-with-replaced-column/61545>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [May 20, 2021, 10:35pm UTC](https://discourse.julialang.org/t/apply-a-function-in-place-to-a-dataframe-column-and-return-a-new-dataframe-with-replaced-column/61545 "2021-05-20T22:35:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon71627001](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@anon71627001](https://discourse.julialang.org/u/anon71627001)\
**Post date:** [May 20, 2021, 10:35pm UTC](https://discourse.julialang.org/t/apply-a-function-in-place-to-a-dataframe-column-and-return-a-new-dataframe-with-replaced-column/61545/1 "2021-05-20T22:35:55Z")

</div>

Hi, is there a function to apply a function to each value in specific column of a DataFrame, and then return a DataFrame with the new column replacing the old column even if it has a different eltype? Something like

`mutate(df, col, my_function_that_operates_on_each_val_of_col)`

For example, starting with

`DataFrame(a = 1:3, b = ["2019 January", "2020 December", "2014 July"])`

and using a function I made that operates on a single string to go to

`DataFrame(a = 1:3, b = [Date(2019-01-01), Date(2020-12-01), Date(2014-07-01)])`

I know I could do it using `my_func.(df[:, :b])` but that doesn’t fit in a pipeline where each step returns a dataframe. I’ve looked at [`mapcols`](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.mapcols) and [`transform`](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.transform) and they don’t seem to fit.

I’d also be fine with a function that operates on a column and just adds the new column to the dataframe so I can drop the old column manually, if it’s not possible to change the datatype of an existing column.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 20, 2021, 11:04pm UTC](https://discourse.julialang.org/t/apply-a-function-in-place-to-a-dataframe-column-and-return-a-new-dataframe-with-replaced-column/61545/2 "2021-05-20T23:04:11Z")

</div>

I don’t think that this is really an in-place operation as you are changing the type. Regardless,

`transform!(df,"b" => ByRow(my_func) => "b")`

should do what you want.
