Hi all,
I have an array of objects that I would like to convert to a DataFrame. Each object has some arrays and some individual values. I would like to destructure the arrays into individual rows and columns and repeat the individual values while maintaining their types. Here is a sample example:
using DataFrames
mutable struct M
id::Int
vals::Array{<:Number,2}
end
ms = [M(i, rand(2,2)) for i in 1:2]
df = DataFrame(ms)
output
Row β id vals
β Int64 Arrayβ¦
ββββββΌββββββββββββββββββββββββββββββββββββββββββ
1 β 1 [0.618182 0.00825483; 0.646895 0β¦
2 β 2 [0.450556 0.462105; 0.889429 0.8β¦
After exploring various approaches this is the closest solution:
rows = mapreduce(x -> [fill(x.id, size(x.vals,1)) x.vals], vcat, ms)
new_df = DataFrame(rows, :auto)
output
4Γ3 DataFrame
Row β x1 x2 x3
β Float64 Float64 Float64
ββββββΌβββββββββββββββββββββββββββββββ
1 β 1.0 0.618182 0.00825483
2 β 1.0 0.646895 0.0341765
3 β 2.0 0.450556 0.462105
4 β 2.0 0.889429 0.843017
The main problem is that x1 is not a row of Int. Is there a better/less hacky way to achieve this goal?
I should also note that I do not know the width of the array vals
. So I cannot hard code column types. However, they will be the same width for each object.