df = DataFrame(ID=1:4,X=11:14, Y=10:13)
indexing to one colum worked using this - Dict(pairs(df.x))
I wanted to convert this to Dict in julia to get like
1 => [ 11,10]
2 => [12,11]
3 => [13,12]
4 => [14,13]
Tried Dict(pairs([df.x,df.y])), shows error
Is there any other way to get this done
julia> Dict(df.ID[i] => [df.X[i], df.Y[i]] for i in 1:nrow(df))
Dict{Int64, Vector{Int64}} with 4 entries:
4 => [14, 13]
2 => [12, 11]
3 => [13, 12]
1 => [11, 10]
3 Likes
Another alternative:
julia> Dict(i => collect(u) for (i,u) in enumerate(zip(df.X,df.Y)))
Dict{Int64, Vector{Int64}} with 4 entries:
4 => [14, 13]
2 => [12, 11]
3 => [13, 12]
1 => [11, 10]
4 Likes
And using Julia broadcasting for compactness:
Dict(df.ID .=> eachrow([df.X df.Y]))
4 => [14, 13]
2 => [12, 11]
3 => [13, 12]
1 => [11, 10]
6 Likes
This might also be useful to you. It will work even if the number of columns change. A DataFrameRow
works just like a Vector
except you can also index into it by column name.
julia> d = Dict(df.ID .=> eachrow(df[:, Not(:ID)]))
Dict{Int64, DataFrameRow{DataFrame, DataFrames.Index}} with 4 entries:
4 => DataFrameRow…
2 => DataFrameRow…
3 => DataFrameRow…
1 => DataFrameRow…
julia> d[1]
DataFrameRow
Row │ X Y
│ Int64 Int64
─────┼──────────────
1 │ 11 10
julia> d[1][2]
10
julia> d[1].Y
10
3 Likes
julia> Dict(zip(df.ID,zip(df.X, df.Y)))
Dict{Int64, Tuple{Int64, Int64}} with 4 entries:
4 => (14, 13)
2 => (12, 11)
3 => (13, 12)
1 => (11, 10)
julia> Dict(zip(df.ID,zip(eachcol(df[:,Not(:ID)])...)))
Dict{Int64, Tuple{Int64, Int64}} with 4 entries:
4 => (14, 13)
2 => (12, 11)
3 => (13, 12)
1 => (11, 10)
1 Like