# Sequentially add data to a DataFrame

**URL:** https://discourse.julialang.org/t/sequentially-add-data-to-a-dataframe/12927
**Category:** New to Julia
**Tags:** question, dataframes
**Created:** [August 6, 2018, 7:33am UTC](https://discourse.julialang.org/t/sequentially-add-data-to-a-dataframe/12927 "2018-08-06T07:33:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [August 6, 2018, 7:33am UTC](https://discourse.julialang.org/t/sequentially-add-data-to-a-dataframe/12927/1 "2018-08-06T07:33:55Z")

</div>

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:

```julia
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!

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 6, 2018, 8:38am UTC](https://discourse.julialang.org/t/sequentially-add-data-to-a-dataframe/12927/2 "2018-08-06T08:38:35Z")

</div>

I use exactly the same pattern except that I create a `DataFrame` like this:

```julia
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.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [August 6, 2018, 8:43am UTC](https://discourse.julialang.org/t/sequentially-add-data-to-a-dataframe/12927/3 "2018-08-06T08:43:09Z")

</div>

Of course! Thanks.

> [@bkamins](#):
>
> 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.

Could you give an example, showcasing the appropriate syntax?

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 6, 2018, 8:45am UTC](https://discourse.julialang.org/t/sequentially-add-data-to-a-dataframe/12927/4 "2018-08-06T08:45:57Z")

</div>

```julia
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.

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [January 9, 2025, 1:47pm UTC](https://discourse.julialang.org/t/sequentially-add-data-to-a-dataframe/12927/5 "2025-01-09T13:47:50Z")

</div>

I’m sorry for necroing a thread that is over 6 years old, but I want to add that the comment marked “solution” will throw an error.

For an empty `DataFrame`, bkamins’ first comment works, but for an unitialised `DataFrame` of length `N`, as explained in [another thread](https://discourse.julialang.org/t/how-to-initialize-empty-dataframe-of-specified-size/67421/4), it’s far better to construct and populate the individual columns as length `N` `Vector`s first, then construct the DataFrame from these vectors.
