I have a DataFrame
and I would like to store the data in an Array
. This constructs the DataFrame
:
using DataFrames
using DataFramesMeta
using StatsBase
n = 9^2*2^2*6^2*42
const df = crossjoin(
DataFrame(am=1:9),
DataFrame(af=1:9),
DataFrame(em=1:2),
DataFrame(ef=1:2),
DataFrame(km=1:6),
DataFrame(kf=1:6),
DataFrame(t=1:42)
)
insertcols!(df, :prob => rand(n))
insertcols!(df, :w => 1)
for i in 1:1000
push!(df, (rand(1:9), rand(1:9), rand(1:2), rand(1:2), rand(1:6), rand(1:6), rand(1:42), rand(), sample(0:1)))
end
This stores the data in an Array
:
const Pdata = Array{Float64}(undef, (9, 2, 9, 2, 6, 6, 42))
# this loop takes a long time
for idx in CartesianIndices(Pdata)
am, em, af, ef, km, kf, t = Tuple(idx)
df1 = @where(df, :t .== t, :am .== am, :af .== af, :em .== em, :ef .== ef, :km .== km, :kf .== kf)
if size(df1, 1) == 1 && df1[1, :w] > 0
Pdata[idx] = df[1, :prob]
elseif size(df1, 1) == 0 || df1[1, :w] == 0
Pdata[idx] = 0.0
end
end
Is there a way to speed up that loop? or is there another way to achieve the “transfer” of data from a DataFrame
to an Array
?