I often need to populate a
DataFrame sequentially, one iteration at a time. I therefore end up initiating a
DataFrame with mock data but with the correct types, deleting that first row, and then iterating through the real data I want to populate it with.
It looks like this:
df = DataFrame(place = "", quantity = 0, when = Date(0))
for i in 1:n
# <do things>
push!(df, (p, q, w))
This works, but I wonder if there is a better way to do this…
I use exactly the same pattern except that I create a
DataFrame like this:
df = DataFrame(place=String, quantity=Int, when=Date)
to avoid deleting the first row later.
Alternatively there are constructors that take type of the data and number of rows and create
DataFrame of an appropriate size and then you do not
push! a row but assign it. But typically I prefer
push! as it is simpler to think of.
Of course! Thanks.
Could you give an example, showcasing the appropriate syntax?
julia> DataFrame([Int, Float64, String], [:a, :b, :c], 3)
│ Row │ a │ b │ c │
│ 1 │ 428567856 │ 2.11741e-315 │ #undef │
│ 2 │ 428567920 │ 5.86393e-316 │ #undef │
│ 3 │ 428567984 │ 5.84443e-316 │ #undef │
julia> DataFrame([Int, Float64, String], [:a, :b, :c], 0)
The second example creates an empty
DataFrame so you can
push! into it like above. The first creates an uninitialized
DataFrame with three rows.