# How to construct a DataFrame with a loop?

**URL:** <https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958>\
**Category:** General Usage\
**Created:** [April 9, 2019, 1:54pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958 "2019-04-09T13:54:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![steph\_de\_paris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steph_de_paris/32/7780_2.png) [@steph\_de\_paris](https://discourse.julialang.org/u/steph_de_paris)\
**Post date:** [April 9, 2019, 1:54pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/1 "2019-04-09T13:54:38Z")

</div>

Hello,

I need to construct a DataFrame similar to:

```julia
df=DataFrame([
            String, String, String, ...
            ], 
            [
            :cod_1, :cod_2, :cod_3, cod_... 
            ],
            0);

```

where the number of String is huge. So, by supposing that this number is N, how to construct df with a loop similar to:

```julia
df=DataFrame([
            for k in 1:N String
            ], 
            [
            for k in 1:N string("cod_",k) 
            ],
            0);

```

Of, course, the previous code does not work. Could you tell me the right way for such a code ? Thanks !

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 9, 2019, 2:10pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/2 "2019-04-09T14:10:21Z")

</div>

Please see [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) (and update your post accordingly).

---

<div class="post-metadata">

**Author:** ![steph\_de\_paris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steph_de_paris/32/7780_2.png) [@steph\_de\_paris](https://discourse.julialang.org/u/steph_de_paris)\
**Post date:** [April 9, 2019, 3:02pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/3 "2019-04-09T15:02:43Z")

</div>

It is done ! Thanks.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [April 9, 2019, 3:48pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/4 "2019-04-09T15:48:02Z")

</div>

I’m not sure, if I missed something, but this is valid code:

```julia
julia> n = 7
7

julia> DataFrame([String for k in 1:n],
                 [Symbol("cod_$k") for k in 1:n],
                 0)
0×7 DataFrame

```

---

<div class="post-metadata">

**Author:** ![steph\_de\_paris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steph_de_paris/32/7780_2.png) [@steph\_de\_paris](https://discourse.julialang.org/u/steph_de_paris)\
**Post date:** [April 9, 2019, 5:37pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/5 "2019-04-09T17:37:52Z")

</div>

> [@zweiglimmergneis](#):
>
> DataFrame([String for k in 1:n], [Symbol(“cod\_$k”) for k in 1:n], 0)

Thanks ! It works ! Nevertheless, how to use it when we need to have Date and String. More precisely, this _bad_ code does not work:

```julia
DataFrame([Date, String for k in 1:2],
                 [:val_date, Symbol("cod_$k") for k in 1:2],
                 0)

```

Of course,

```julia
DataFrame([Date, String, String, ...],
                 [:val_date, :cod_1, :cod_2, ...],
                 0)

```

is OK.

I found a solution by using 2 dataframes and the join function:

```julia
df_1=DataFrame([Date, String],
                 [:val_date, :cod_1],
                 0)
df_2=DataFrame([String for k in 1:7],
                 [Symbol("cod_$k") for k in 1:7],
                 0)
df=join(df_1,df_2,on=:cod_1)

```

But it seems to me that it could be more simple. Is it the case ?

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [April 9, 2019, 8:47pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/6 "2019-04-09T20:47:16Z")

</div>

For passing the `Vector{DataType}`,

```julia
vcat(Date, fill(String, 2)) # Example [Date, String, String]

```

You can use `vcat` for the names as well.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [April 9, 2019, 9:01pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/7 "2019-04-09T21:01:33Z")

</div>

Yes, and eventually think of using the broadcast feature for the column names.

```julia
julia> using DataFrames, Dates

julia> n = 7
7

julia> makecol(k) = Symbol("cod_", k)
makecol (generic function with 1 method)

julia> df = DataFrame([Date, fill(String, n)...],
                      [:valdate, makecol.(1:n)...],
                      0)
0×8 DataFrame

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 9, 2019, 10:54pm UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-with-a-loop/22958/8 "2019-04-09T22:54:29Z")

</div>

For a more general way to do this, you can create an “empty” DataFrame with the schema that you want and then just `push!` Dicts or Named Tuples to add observations to it. I have done this with fair amounts of success when the creation of the DataFrame is less trivial.
