iHany
June 25, 2021, 3:43am
1
Hi,
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
?
nilshg
June 25, 2021, 5:55am
2
If I understand you correctly your comprehension should be [i[:x] for i in df.data]
1 Like
iHany
June 25, 2021, 6:12am
3
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 df.data]
.
iHany
June 25, 2021, 6:29am
5
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 ...?
iHany:
# 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.(df.data, :y), :a)
2-element Vector{Int64}:
1
2
1 Like
iHany
June 25, 2021, 7:16am
7
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
iHany
June 25, 2021, 7:45am
9
Oh, I’ve seen it. Ok, it may be the key of my issue
I’ll take a look at it. Thanks!