I would like to know, how may I export a nested dictionary
directly to a CSV file
in Julia?
For example: I have a following dictionary ->
d = Dict(:a => Dict(:val1 => rand(4), :val2 => rand(450)),
:b => Dict(:val1 => rand(4), :val2 => rand(1500)));
The issue is that the following has irregular size, hence difficult to convert to dataframes. Is there any direct alternative for exporting such irregular dictionaries.
Thanks!
You can change your nested keys to columns of the CSV file, though that will waste disk space and memory, since you then repeat all your keys in each row, with one row per bottom level datum:
using DataFrames
const d = Dict(:a => Dict(:val1 => rand(4), :val2 => rand(450)),
:b => Dict(:val1 => rand(4), :val2 => rand(1500)));
const alen, blen = 4 + 450, 4 + 1500
df = DataFrame(
letters = vcat(fill(:a, alen), fill(:b, blen)),
vals = vcat(fill(:val1, 4), fill(:val2, 450),
fill(:val1, 4), fill(:val2, 1500)),
rands = vcat(values(d[:a][:val1]), values(d[:a][:val2]),
values(d[:b][:val1]), values(d[:b][:val2]))
)
@show filter(r -> r.letters == :a && r.vals == :val1, df)
CSV.write(pathname, df)
This uses several extra bytes per random value in your example, since each row has redundant entries starting like a,val1,
. So JLD2 would be better for space reasons unless you are feeding the CSV to a program that requires that format.