I have an array of 31 arrays and would like to make it a DataFrame. Need some help

Hi how’s it going,

I have an array of 31 arrays, each with 3 items in it. first two items are strings, third is a float. I just want to transform this into a Julia DataFrame and export to CSV. Just calling the DataFrame method gave me a 1 column dataframe with the entire row as one column. Can someone help me with this?

Any help would be greatly appreciated!

Thanks.

Can you post your array?

df = DataFrame()
for i in 1:length(array_of_vectors)
    df[:, Symbol(:x, i)] = array_of_vectors[i]
end

Can you post the results of Pkg.status()?

I am on master and have

julia> DataFrame([rand(3) for i in 1:31]) 
3×31 DataFrame. Omitted printing of 25 columns
│ Row │ x1        │ x2       │ x3        │ x4       │ x5       │ x6       │
│     │ Float64   │ Float64  │ Float64   │ Float64  │ Float64  │ Float64  │
├─────┼───────────┼──────────┼───────────┼──────────┼──────────┼──────────┤
│ 1   │ 0.74393   │ 0.099329 │ 0.394221  │ 0.927078 │ 0.609199 │ 0.929626 │
│ 2   │ 0.850305  │ 0.597505 │ 0.441102  │ 0.37355  │ 0.432169 │ 0.312555 │
│ 3   │ 0.0256715 │ 0.818313 │ 0.0370936 │ 0.763612 │ 0.791267 │ 0.209623 │

Alternatively, you can do

df = DataFrame(col1 = String[], col2 = String[], col3 = Float64[])
for v in array_of_vectors
    push!(df, v)
end

The reason this is a bit awkward is that it’s recommended that you store your “rows”- so to speak, as named tuples rather than vectors. If you store them as named tuples you can just do

df = DataFrame()
for v in arra_of_named_tuples
    push!(df, v)
end

EDIT: furthermore, if you have an array of named tuples you can just call

DataFrame(vector_of_named_tuples)

and it will work thanks to Tables.jl