# Help with filling dataframe

**URL:** <https://discourse.julialang.org/t/help-with-filling-dataframe/55839>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [February 23, 2021, 10:58am UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839 "2021-02-23T10:58:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [February 23, 2021, 10:58am UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/1 "2021-02-23T10:58:24Z")

</div>

I’m having trouble with the code to fill a dataframe with some strings

say the strings are defined as

```julia
data = ["a\tb","c\td","e\tf"]

```

a dataframe is preallocated as

```julia
df = DataFrame([String, String],[:X,:Y],3)

```

I’m trying to fill the dataframe like this, but it doesn’t seem to be working. Is there something basic I’m doing wrong?

```julia
df = split.(data, "\t")

```

I’ve also tried this, but this doesn’t work either.

```julia
for i in 1:length(data)
     df[i,:] = split(data[i], "\t")
end

```

I don’t find any examples in the documentation that covers this use case.

---

<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 23, 2021, 11:06am UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/2 "2021-02-23T11:06:19Z")

</div>

> [@jonjilla](#):
>
> `df = DataFrame([String, String],[:X,:Y],3)`

Please note that this is a deprecated syntax (use `--depwarn=yes` at startup to see warnings)

Now the way to get what you want is e.g.:

```julia
julia> rename(DataFrame(Tuple.(split.(data))), [:X, :Y])
3×2 DataFrame
 Row │ X Y
     │ SubStrin… SubStrin…
─────┼──────────────────────
   1 │ a b
   2 │ c d
   3 │ e f

```

or

```julia
julia> select(DataFrame(data=data), :data => ByRow(split) => [:X, :Y])
3×2 DataFrame
 Row │ X Y
     │ SubStrin… SubStrin…
─────┼──────────────────────
   1 │ a b
   2 │ c d
   3 │ e f

```

or

```julia
julia> s = split.(data)
3-element Vector{Vector{SubString{String}}}:
 ["a", "b"]
 ["c", "d"]
 ["e", "f"]

julia> DataFrame(X=getindex.(s, 1), Y=getindex.(s, 2))
3×2 DataFrame
 Row │ X Y
     │ SubStrin… SubStrin…
─────┼──────────────────────
   1 │ a b
   2 │ c d
   3 │ e f

```

whichever seems easier for you to use.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [February 23, 2021, 3:54pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/3 "2021-02-23T15:54:49Z")

</div>

@bkamins Thanks for your help, I was actually right in the middle of your course on DataFrames on JuliaAcademy so it’s nice to get an answer from you.

I didn’t realize that method of preallocating DataFrames was deprecated.

From your suggestions, how would you define the data types using that syntax? In this case, it is easy because they are all strings, but what about if I wanted to define those types up front?

---

<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 23, 2021, 6:30pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/4 "2021-02-23T18:30:26Z")

</div>

I assume you are asking how to create a data frame with uninitialized columns of certain types with a certain number of rows. Then you can do e.g.:

```julia
DataFrame([n => Vector{T}(undef, 3) for (n, T) in [:X => String, :Y => Int]])

```

or

```julia
DataFrame([:X, :Y] .=> [Vector{T}(undef, 3) for T in [String, Int]])

```

or

```julia
DataFrame([Vector{T}(undef, 3) for T in [String, Int]], [:X, :Y])

```

I know this is not super friendly to type, but this has a reason. If it not efficient to create a data frame this way and later fill it with data. That is why it is also discouraged on a syntax level. It is usually better to construct a data frame dynamically as data is added to it.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [February 23, 2021, 7:09pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/5 "2021-02-23T19:09:23Z")

</div>

@bkamins OK, I’ll try it the way you previously suggested. From the documentation, it says that row by row construction of a dataframe is not performant, so I was going with this preallocation, but it seems to be fast enough for what I need.

Thanks for your help!

---

<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 23, 2021, 7:33pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/6 "2021-02-23T19:33:28Z")

</div>

> [@jonjilla](#):
>
> From the documentation, it says that row by row construction of a dataframe is not performant

Could you please pass me a link to this part of documentation? Thank you!

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [February 23, 2021, 7:44pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/7 "2021-02-23T19:44:17Z")

</div>

@bkamins From this link: [Getting Started · DataFrames.jl](https://dataframes.juliadata.org/stable/man/getting_started/)

“Note that constructing a `DataFrame` row by row is significantly less performant than constructing it all at once, or column by column. For many use-cases this will not matter, but for very large `DataFrame` s this may be a consideration.”

---

<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 23, 2021, 7:53pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/8 "2021-02-23T19:53:21Z")

</div>

Ah this is correct as this is compared to “creating is all at once” or “column by column” (which are both faster).

The option to leave entries of a data `#undef` and then fill them is usually slowest.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [February 23, 2021, 10:10pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/9 "2021-02-23T22:10:25Z")

</div>

OK, I see. Thanks for the clarification.

One final question, would it be possible to have an array of tab delimited strings like in my example and then use that to create a dataframe column by column?

---

<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 23, 2021, 10:39pm UTC](https://discourse.julialang.org/t/help-with-filling-dataframe/55839/10 "2021-02-23T22:39:31Z")

</div>

But you mean that each string holds one row? If yes - then all three answers I have given in my initial reply work this way.
