# Best way to iteratively add to a DataFrame?

**URL:** <https://discourse.julialang.org/t/best-way-to-iteratively-add-to-a-dataframe/21084>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [February 22, 2019, 3:56pm UTC](https://discourse.julialang.org/t/best-way-to-iteratively-add-to-a-dataframe/21084 "2019-02-22T15:56:08Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [February 22, 2019, 4:52pm UTC](https://discourse.julialang.org/t/best-way-to-iteratively-add-to-a-dataframe/21084/4 "2019-02-22T16:52:21Z")

</div>

The solution is correct, but I have some minor additional notes.

`reduce(vcat, [DataFrame(a = rand(i)) for i in 1:5])`

is only minimally faster than

`vcat([DataFrame(a = rand(i)) for i in 1:5]...)`

(the change was merged yesterday to master and has not been released yet (earlier splatting was the recommended approach).

Also creating intermediate data frames is not efficient. The recommended way to add rows to a data frame is:

```julia
using DataFrames
dflong = DataFrame(a=Float64[])
for i = 1:3
    push!(dflong, (rand(i),))
end

```

(you can read the documentation of `push!` to find the accepted types of rows, in particular you can `push!` a `NamedTuple`, a dictionary, a vector or a tuple)

If you really have to create intermediate `DataFrame`s then you can also do it with `append!` which will also be relatively fast (and you do not have to store all the data frames in the memory before `vcat`-ing):

```julia
using DataFrames
dflong = DataFrame(a=Float64[])
for i = 1:3
    append!(dflong, DataFrame(a=rand(i)))
end

```

---

_[View the full topic](https://discourse.julialang.org/t/best-way-to-iteratively-add-to-a-dataframe/21084)._
