I have a vector with a custom type (struct).
Suppose I want the data in a DataFrame. Can I do this more efficiently than below? (i.e. without copying data)
Of course, I could generate a DataFrame in the beginning (instead of a Vector of some type). But a struct provides more flexibility and can hold, e.g., a Matrix as well.
using BenchmarkTools
using DataFrames
struct Mine
a::Int
b::Float64
c::Matrix{Float64}
end
myVector=Vector{Mine}()
for i=1:1000000
push!(myVector,Mine(rand(1:10),rand(),rand(2,2)))
end
function flatten_to_DF(x)
df=DataFrame()
for h in ["a","b"]
hSymbol=Symbol(h)
df[hSymbol]=map(z->getfield(z,hSymbol),x)
end
return df
end
@btime d=flatten_to_DF(myVector)