How to make empty dataframe with column names?

df = DataFrame(x = [], y = String[])
0×2 DataFrame

I know that empty dataframe with column names, but if i have long column names below, how can i make the empty dataframe?

julia>            nodenames
224-element Vector{String}:
 "American Samoa"
 "Virgin Islands"
 "Wallis and Futuna"
 "Western Sahara"

Here is one form.

julia> df = DataFrame([[],[],[]], ["one", "two", "three"])
0×3 DataFrame

julia> push!(df, [1, 2, 3])
1×3 DataFrame
 Row │ one  two  three 
     │ Any  Any  Any   
   1 │ 1    2    3

Thank you! For others, i suggest below:

julia> DataFrame([[] for _ = nodenames] , nodenames)
0×224 DataFrame
1 Like

A more convenient way to do it might be:

DataFrame([name => [] for name in nodenames])

Note that this way you create columns with Any eltype. Maybe you would prefer more concrete eltypes, e.g.:

DataFrame([name => Int[] for name in nodenames])
1 Like

(I don’t know if they are preferable in any way to the other proposals.
just to show a slightly different way)


DataFrame(Matrix{Int64}(undef, 0,length(nodenames)), nodenames)

or use empty!(df) function if you already have some similar df

This pattern is correct, but I would not recommend it as fill(Int[],length(nodenames)) reuses the Int[] vector. The only reason why this works properly is that, by default DataFrame constructor copies columns.

This is the reason why I have not shown the option:

DataFrame(nodenames .=> Ref([]))

which is short, but it has the same flaw.

To show you what I mean by the risk consider the following example:

julia> nodenames = ["a", "b", "c"]
3-element Vector{String}:

julia> df = DataFrame(fill(Int[],length(nodenames)),nodenames; copycols=false)
0×3 DataFrame

julia> push!(df.a, 100)
1-element Vector{Int64}:

julia> df
1×3 DataFrame
 Row │ a      b      c
     │ Int64  Int64  Int64
   1 │   100    100    100
1 Like