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
Eg some variation on
@. df.somecol = round(df.somecol; digits = 2)
2 Likes
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
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.
Note that
@. df.somecol = round(df.somecol; digits = 2)
is not equivalent to
df.somecol = round.(df.somecol; digits = 2)
but to
df.somecol .= round.(df.somecol; digits = 2)
1 Like
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
.
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).
1 Like
Thanks, I did not know this!
In my toy example, replacing =
by .=
gave a speed-up of nearly a factor 2.
1 Like