Sequentially add data to a DataFrame

question
dataframes

#1

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))
deleterows!(df, 1)
for i in 1:n
    # <do things>
    push!(df, (p, q, w))
end

This works, but I wonder if there is a better way to do this…

Thanks!


#2

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.


#3

Of course! Thanks.

Could you give an example, showcasing the appropriate syntax?


#4
julia> DataFrame([Int, Float64, String], [:a, :b, :c], 3)
3×3 DataFrames.DataFrame
│ 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)
0×3 DataFrames.DataFrame

The second example creates an empty DataFrame so you can push! into it like above. The first creates an uninitialized DataFrame with three rows.