# Create dataframe with n columns of strings

**URL:** <https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693>\
**Category:** General Usage\
**Created:** [March 29, 2020, 7:34pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693 "2020-03-29T19:34:17Z")\
**Posts on this page:** 7\
**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:** [March 29, 2020, 7:34pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693/1 "2020-03-29T19:34:17Z")

</div>

I’m sure there is a simple way to do this, but I cannot figure this out.

Is there a simple way to create a dataFrame of n columns of all String type?

I know I can do this

```julia
DataFrame([String,String,String],[:x1, :x2, :x3],0)

```

but if i have a varying number of columns, I would like to be able to do this dynamically from the size of the input data source.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 29, 2020, 7:52pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693/2 "2020-03-29T19:52:09Z")

</div>

To answer your question directly, you can do

```julia
df = DataFrame([String[] for i in 1:n])

```

More generally, remember that you can initialize an empty data frame and add columns to it in a loop.

```julia
df = DataFrame()
for col in container_of_cols
    df[:, name_of_col] = col
end

```

You can also initialize an empty DataFrame and `push!` named tuples to it. The types will work out as needed.

```julia
julia> df = DataFrame()
0×0 DataFrame

julia> push!(df, (a = "hey", b = "there"))

```

---

<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:** [March 29, 2020, 8:01pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693/3 "2020-03-29T20:01:41Z")

</div>

Thanks for your help, I knew it should have been something simple.

---

<div class="post-metadata">

**Author:** ![Luigi\_Bottecchia](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Luigi\_Bottecchia](https://discourse.julialang.org/u/Luigi_Bottecchia)\
**Post date:** [February 4, 2021, 4:09pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693/4 "2021-02-04T16:09:03Z")

</div>

Hi! I’m using Julia v1.3 and DataFrames v0.22.5 and I found out that this solution is not working anymore.

Before I was using the following code:

```julia
             df = DataFrame()
             for c in col_names_el
                 df[Symbol(c)] = 0.0
             end

```

But now also this is not working. Any Tips on how to solve this?

P.s.  
Not sure if I should open a new topic so in case let me know and I will do that!

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 4, 2021, 4:24pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693/5 "2021-02-04T16:24:40Z")

</div>

Are you sure this was on DataFrames 0.22? I feel like this syntax has been deprecated longer ago… Anyway: you need to index a DataFrame with both row and column index, so just `df[:colname]` is not allowed anymore, you need `df[!, :colname]`. You also want to broadcast the assignment of the scalar `0.0` to the column, so use:

```julia
for c in col_names_el
    df[!, c] .= 0.0
end

```

Note that it’s also not necessary to do `Symbol(c)` anymore, as DataFrames can now be indexed with strings (although symbols continue to work as well, so no need to rewrite code that uses symbols!)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 4, 2021, 4:29pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693/6 "2021-02-04T16:29:47Z")

</div>

The following works

```julia
julia> df = DataFrame();

julia> for c in ["a", "b"]
           df[:, c] = [0.0]
       end

```

Two things.

1. as mentioned by @nilshg, you need to use either `!` or `:` to index into a column
2. `0.0` is a scalar. DataFrames columns are vectors. So you need to assign a vector to the column of the data frame.

---

<div class="post-metadata">

**Author:** ![Luigi\_Bottecchia](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Luigi\_Bottecchia](https://discourse.julialang.org/u/Luigi_Bottecchia)\
**Post date:** [February 4, 2021, 4:33pm UTC](https://discourse.julialang.org/t/create-dataframe-with-n-columns-of-strings/36693/7 "2021-02-04T16:33:54Z")

</div>

Thanks @pdeffebach and @nilshg ! Before I was using V0.21 and the code I provided was working. I got little confused by the error and actually forgot the . in front of the =

Thank you again 🙂
