Nested access of DataFrame


I’m looking forward to dealing with nested access of DataFrame.
For example,

julia> using DataFrames

julia> xs = [Dict(:x => 1, :y => 1)]
1-element Vector{Dict{Symbol, Int64}}:
 Dict(:y => 1, :x => 1)

julia> push!(xs, Dict(:x => 2, :y => 2))
2-element Vector{Dict{Symbol, Int64}}:
 Dict(:y => 1, :x => 1)
 Dict(:y => 2, :x => 2)

julia> df = DataFrame(data=xs)
2×1 DataFrame
 Row │ data
     │ Dict…
   1 │ Dict(:y=>1, :x=>1)
   2 │ Dict(:y=>2, :x=>2)

Now, I’d like to access data as [df[i][:x] for i in 1:size(df)[1]] and [df[i][:y] for i in 1:size(df)[1]].
Is there any convenient solution like df.x?

If I understand you correctly your comprehension should be [i[:x] for i in]

1 Like

Oh, you’re correct.
In addition to this, I wish that there is an alternative (and easy) way, e.g., df.x == [i[:x] for i in].

Do you want:

getindex.(, :x)



Thanks for replying.
The way you suggested seems not work for nested dict.
For example,

xs = [Dict(:x => 1, :y => Dict(:a => 1))]
push!(xs, Dict(:x => 2, :y => Dict(:a => 2))
# df.y.a ...?

you cannot do a getproperty on Dict, instead to have to do a getindex as I have suggested. It is unrelated with DataFrame object, but how Julia Base is designed:

julia> using DataFrames

julia> xs = [Dict(:x => 1, :y => Dict(:a => 1))];

julia> push!(xs, Dict(:x => 2, :y => Dict(:a => 2)));

julia> df = DataFrame(data=xs);

julia> getindex.(getindex.(, :y), :a)
2-element Vector{Int64}:
1 Like

Oh, good point.
But it is too long to access data in nested dicts.
As you suggested, it would seem like
getindex.(getindex.(getindex. ....
This is quite annoying. So I’m looking forward to any methods or packages that dealing with such nested dicts in easy ways.

GitHub - JuliaArrays/StructArrays.jl: Efficient implementation of struct arrays in Julia should be useful in your case (it does not resolve all your issues, but still should help).

1 Like

Oh, I’ve seen it. Ok, it may be the key of my issue :slight_smile:
I’ll take a look at it. Thanks!