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}:
 "Afghanistan"
 "Albania"
 "Algeria"
 "American Samoa"
 "Angola"
 "Anguilla"
 ⋮
 "Virgin Islands"
 "Wallis and Futuna"
 "Western Sahara"
 "Yemen"
 "Zambia"
 "Zimbabwe"
3 Likes

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
4 Likes

Thank you! For others, i suggest below:

julia> DataFrame([[] for _ = nodenames] , nodenames)
0×224 DataFrame
3 Likes

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])
8 Likes

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

DataFrame(fill(Int[],length(nodenames)),nodenames)

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}:
 "a"
 "b"
 "c"

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

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

julia> df
1×3 DataFrame
 Row │ a      b      c
     │ Int64  Int64  Int64
─────┼─────────────────────
   1 │   100    100    100
2 Likes