Round values for dataset

x=1.000001
df=DataFrame(A=x)
for i in 1:10
x=x+1
push!(df,(x))
end

Is there a way to round this to say, 5 decimal places?, and to have that rounding thougout the dataframe?

You want

floor(x, sigdigits = 5)
1 Like

where does that do I add that?

I don’t know, wherever you would like. perhaps at the beginning?

x = round(1.00001, sigdigits = 5)
1 Like

Here is an example with two values, one rounds down the other rounds up.
I am showing this rounding to four digits after the decimal point rather than five digits, so Julia will display all digits even when showing them in compact mode. To do the same rounding to five digits after the decimal point, just change 4 to 5 in this line: const fractional_digits = 4.

using DataFrames
const fractional_digits = 4
x1 = 2.00004
x2 = 3.99996
xs = [x1, x2]
df = DataFrame(X=xs)
#  the revised `:X` column gets assigned the values
#   in the original `:X` column after rounding
df[!, :X] = round.(df[:, :X], digits=fractional_digits)
# notice the `.` after `round`, the `:` before `X`
#   and notice the use of the keyword `digits`

which when run gives you

julia> using DataFrames
julia> const fractional_digits = 4
4
julia> x1 = 2.00004
2.00004
julia> x2 = 3.99996
3.99996
julia> xs = [x1, x2]
2-element Array{Float64,1}:
 2.00004
 3.99996

julia> df = DataFrame(X=xs)
2Γ—1 DataFrame
 Row β”‚ X
     β”‚ Float64
─────┼─────────
   1 β”‚ 2.00004
   2 β”‚ 3.99996

julia> # notice the `.` after `round`, the `:` before `X`
julia> #  and notice the use of the keyword `digits`
julia> df[!,:X] = round.(df[:,:X], digits=fractional_digits)
2-element Array{Float64,1}:
 2.0
 4.0

julia> df
2Γ—1 DataFrame
 Row β”‚ X
     β”‚ Float64
─────┼─────────
   1 β”‚     2.0
   2 β”‚     4.0

Feel free to ask for an explanation of any part of that.

Is there supposed to be a period after digits?

No. (assuming you are asking about the keyword argument digits that is used in the round function). I have edited the comments above to make it clearer.

No. (assuming you are asking about the assignment to fractional_digits). :slight_smile:

yes, I figured you tested it, and when I copied your code it worked.

It is good to have working code. It is better to have working code that you understand – so keep asking questions.

1 Like

What does const fractional digits do?

What if I want 2 columns?

It just lets you use fractional_digits as a variable name in this line
df[!, :X] = round.(df[:, :X], digits=fractional_digits)

This is better practice than hard-coding the number 4 there.

show me the two columns you want

makes sense