# Creating an empty dataframe from a vector of strings fails?

**URL:** <https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [April 25, 2022, 7:12pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008 "2022-04-25T19:12:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [April 25, 2022, 7:12pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/1 "2022-04-25T19:12:10Z")

</div>

Hi all  
got a string vector

```julia
col_names = ["start","interval","goal","num_hedgeHogs"]

```

I want to create an empty dataframe using col\_names as the column names and a data type of Any.

I thought this might do the trick

```julia
 df_test = DataFrame( Symbol.(col_names))

```

but it get’s me

```julia
ERROR: ArgumentError: 'Vector{Symbol}' iterates 'Symbol' values, which doesn't satisfy the Tables.jl `AbstractRow` interface
Stacktrace:
 [1] invalidtable(#unused#::Vector{Symbol}, #unused#::Symbol)
   @ Tables ~/.julia/packages/Tables/PxO1m/src/tofromdatavalues.jl:42
 [2] iterate
   @ ~/.julia/packages/Tables/PxO1m/src/tofromdatavalues.jl:48 [inlined]
 [3] buildcolumns
   @ ~/.julia/packages/Tables/PxO1m/src/fallbacks.jl:197 [inlined]
 [4] columns
   @ ~/.julia/packages/Tables/PxO1m/src/fallbacks.jl:260 [inlined]
 [5] DataFrame(x::Vector{Symbol}; copycols::Nothing)
   @ DataFrames ~/.julia/packages/DataFrames/MA4YO/src/other/tables.jl:58
 [6] DataFrame(x::Vector{Symbol})
   @ DataFrames ~/.julia/packages/DataFrames/MA4YO/src/other/tables.jl:49
 [7] top-level scope
   @ REPL[43]:1

```

and

```julia
df_test = DataFrame(Any[],Symbol.(col_names))

```

gets

```julia
julia> df_test = DataFrame(Any[],Symbol.(col_names))
ERROR: DimensionMismatch("Number of columns (0) and number of column names (9) are not equal")
Stacktrace:
 [1] DataFrame(columns::Vector{AbstractVector}, colindex::DataFrames.Index; copycols::Bool)
   @ DataFrames ~/.julia/packages/DataFrames/MA4YO/src/dataframe/dataframe.jl:178
 [2] DataFrame(columns::Vector{Any}, cnames::Vector{Symbol}; makeunique::Bool, copycols::Bool)
   @ DataFrames ~/.julia/packages/DataFrames/MA4YO/src/dataframe/dataframe.jl:322
 [3] DataFrame(columns::Vector{Any}, cnames::Vector{Symbol})
   @ DataFrames ~/.julia/packages/DataFrames/MA4YO/src/dataframe/dataframe.jl:319
 [4] top-level scope
   @ REPL[5]:1

julia> 

```

any help appreciated .

---

<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:** [April 25, 2022, 7:40pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/2 "2022-04-25T19:40:09Z")

</div>

You would need

```julia
DataFrame([Any[] for i in 1:length(col_names)], col_names)

```

This has the unfortunate consequence of making your columns of type `Any`, which is not ideal.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [April 25, 2022, 7:52pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/3 "2022-04-25T19:52:56Z")

</div>

first of all thank you for the speedy reply. I want to set myself a decent foundation in julia so if there is another approach which is more rational I am more than happy to listen.

secondly. I understand that defining all the columns as Any is a bad move but how do I stipulate the type of the columns? So in my case ( for example)

```julia
"start" is a timestamp
"interval" is a int63
"goal" is a string 
"num_hedgeHogs" is an integer.

```

---

<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:** [April 25, 2022, 8:15pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/4 "2022-04-25T20:15:17Z")

</div>

You can declare empty arrays of a type like `String[]`, or `Int[]` or `DateTime[]`.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [April 25, 2022, 8:23pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/5 "2022-04-25T20:23:37Z")

</div>

but in my case I am creating the dataframe from a string vector for the column names, how would I define the column types using

```julia
DataFrame([Any[] for i in 1:length(col_names)], col_names)

```

Is there a better approach?

---

<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:** [April 25, 2022, 8:54pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/6 "2022-04-25T20:54:59Z")

</div>

First, you can take advantage of the fact that `DataFrame` constructor by default copies columns so you can write:

```julia
DataFrame(col_names .=> Ref([]))

```

Now if you want different element types for columns you can write e.g.:

```julia
DataFrame(col_names .=> [T[] for T in [Int, String, Bool, Char]])

```

or (via a small hack):

```julia
DataFrame(col_names .=> rand.([Int, String, Bool, Char], 0))

```

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [April 25, 2022, 9:10pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/7 "2022-04-25T21:10:41Z")

</div>

thanks for the reply. As always lashings to build on. I didn’t know anything about Ref() so that’s something to look into.  
Regarding

```julia
DataFrame(col_names .=> rand.([Int, String, Bool, Char], 0))

```

Is this a trap 🙂 to my noob eye this seems to randomize the allocation of a type to a column. Admittedly this would add a bit of fun to the process but…

---

<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:** [April 25, 2022, 9:37pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/8 "2022-04-25T21:37:11Z")

</div>

> [@anon69491625](#):
>
> Is this a trap

No, it is not a trap and it is fully non-random (that is why I say it is a hack). We create 0-length vectors of types specified by the first argument:

```julia
julia> rand.([Int, String, Bool, Char], 0)
4-element Vector{Vector}:
 Int64[]
 String[]
 Bool[]
 Char[]

```

The trick is that since the length of all vectors is `0` it works even for data types that are not supported by random number generator.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [April 25, 2022, 9:55pm UTC](https://discourse.julialang.org/t/creating-an-empty-dataframe-from-a-vector-of-strings-fails/80008/9 "2022-04-25T21:55:10Z")

</div>

just checking after the “get us a bubble for the spirit level” incident 🙂

thanks again for the considered reply and the feast of food for thought.
