Empty dataframe in Julia add rows

87 views Asked by At

I'm new in Julia.

Using package DataFrames I created an empty dataframe.

using DataFrames

dt = DataFrame(x=Real[], y=Real[], w=Real[], z=Real[])
0×4 DataFrame
 Row │ x     y     w   z
     │ Real  Real  Real  Real
─────┴─────────────────────────

Now I want to fill this dataframe with zeros and then change the values via a "for". So, I tried

n = 10000
for i in 1:n
  push!(dt[i,:], [0])
end

but I get

ERROR: BoundsError: attempt to access 0×4 DataFrame at index [1, :]

How can I fill the dataframe with zeros?

Thanks in advance

2

There are 2 answers

5
BallpointBen On

You can push rows to the DataFrame as follows:

julia> df = DataFrame(a=Float64[], b=Float64[])
0×2 DataFrame
 Row │ a        b       
     │ Float64  Float64 
─────┴──────────────────

julia> push!(df, (; a=1, b=2))
1×2 DataFrame
 Row │ a        b       
     │ Float64  Float64 
─────┼──────────────────
   1 │     1.0      2.0

julia> push!(df, [3, 4])
2×2 DataFrame
 Row │ a        b       
     │ Float64  Float64 
─────┼──────────────────
   1 │     1.0      2.0
   2 │     3.0      4.0

But much easier, and almost certainly more performant, is to just construct the DataFrame with 0‘s from the start:

julia> DataFrame(zeros(10000, 4), [:a, :b, :c, :d])
10000×4 DataFrame
   Row │ a        b        c        d       
       │ Float64  Float64  Float64  Float64 
───────┼────────────────────────────────────
     1 │     0.0      0.0      0.0      0.0
     2 │     0.0      0.0      0.0      0.0
     3 │     0.0      0.0      0.0      0.0
     4 │     0.0      0.0      0.0      0.0
     5 │     0.0      0.0      0.0      0.0
     6 │     0.0      0.0      0.0      0.0
     7 │     0.0      0.0      0.0      0.0
     8 │     0.0      0.0      0.0      0.0
   ⋮   │    ⋮        ⋮        ⋮        ⋮
  9994 │     0.0      0.0      0.0      0.0
  9995 │     0.0      0.0      0.0      0.0
  9996 │     0.0      0.0      0.0      0.0
  9997 │     0.0      0.0      0.0      0.0
  9998 │     0.0      0.0      0.0      0.0
  9999 │     0.0      0.0      0.0      0.0
 10000 │     0.0      0.0      0.0      0.0
                           9985 rows omitted
0
Sundar R On

From the comments, it seems like you're trying to accomplish something like this:

if pts == 1
  df = DataFrame(x = rand(Uniform(0, 10000)), 
                 y = rand(Uniform(0, 10000)), 
                 w = rand(Uniform(-2*pi, 2*pi)), 
                 z = 1)
elseif pts > 1
  n = pts - 1
  df = DataFrame(x = rand(Uniform(0, 10000), n), 
                 y = rand(Uniform(0, 10000), n), 
                 w = rand(Uniform(-2*pi, 2*pi), n), 
                 z = 1)
end

This way, you can initialize your DataFrame right away with the values you want, instead of zeros initializing them and then overwriting them later.