# Replacing values in a data frame

**URL:** <https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937>\
**Category:** Data\
**Created:** [June 7, 2020, 4:26pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937 "2020-06-07T16:26:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Anderson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_anderson/32/9465_2.png) [@Chris\_Anderson](https://discourse.julialang.org/u/Chris_Anderson)\
**Post date:** [June 7, 2020, 4:26pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/1 "2020-06-07T16:26:31Z")

</div>

Don’t know if this is the right category for this…

I have a dataframe with a column containing averages calculated with the mean function. These values go out several decimal places, but I want to use round() to set it to two or three. Aside from just having populated the dataframe with rounded values to begin with, how can I iterate through the column replacing each value with it’s rounded version? I’ve tried several different ways, .=, .==, replace(), but nothing is replacing the values. Any suggestions?  
Thank you

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 7, 2020, 5:36pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/2 "2020-06-07T17:36:50Z")

</div>

Eg some variation on

```julia
@. df.somecol = round(df.somecol; digits = 2)

```

---

<div class="post-metadata">

**Author:** ![Chris\_Anderson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_anderson/32/9465_2.png) [@Chris\_Anderson](https://discourse.julialang.org/u/Chris_Anderson)\
**Post date:** [June 7, 2020, 6:12pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/3 "2020-06-07T18:12:36Z")

</div>

Thank you very much. I’m pretty new to programming and Julia. I wonder what does the @ do compared to what I had done?  
Thank you

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 7, 2020, 6:24pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/4 "2020-06-07T18:24:50Z")

</div>

Without using `@.`, the following should also do it:  
`df.somecol = round.(df.somecol; digits = 2)`  
`df.somecol` is a vector, and the `.` broadcasts the `round` function to apply to each element of the vector.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 7, 2020, 6:49pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/5 "2020-06-07T18:49:00Z")

</div>

Note that

```julia
@. df.somecol = round(df.somecol; digits = 2)

```

is not equivalent to

```julia
df.somecol = round.(df.somecol; digits = 2)

```

but to

```julia
df.somecol .= round.(df.somecol; digits = 2)

```

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 7, 2020, 6:58pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/6 "2020-06-07T18:58:28Z")

</div>

Correct, but in this context (as far as I can see), `.=` does the same as `=` because both sides already have the same length due to broadcasting of `round`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 7, 2020, 7:00pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/7 "2020-06-07T19:00:32Z")

</div>

If he is using `DataFrames.jl` I think that `=` will replace the old vector by a new one, while `.=` will alter the old vector object inplace (and because broadcast fusing this will probably not allocate memory).

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 7, 2020, 7:11pm UTC](https://discourse.julialang.org/t/replacing-values-in-a-data-frame/40937/8 "2020-06-07T19:11:13Z")

</div>

Thanks, I did not know this!  
In my toy example, replacing `=` by `.=` gave a speed-up of nearly a factor 2.
