Create a dictionary from Dictionary and Array

Hello everyone,

I have an array a = [8,9] and a dictionary d = Dict(“c” = [1,3,4,6] , “b” = [2,5])
How can I assign all the values of “c” and “b” to the corresponding values of “c” and “b” in a?
Result : h = Dict(“c” = [8,8,8,8] , “b” = [9,9])

Thank you,

How do you know that "c" should correspond to the 8 and "b" to the 9?

1 Like

df1 = DataFrame(Column1 = [“c”,“b”,“c”,“c”,“b”,“c”],
Column2 = [“Value1”,“Value2”,“Value3”,“Value1”,“Value2”,“Value3”],
Column3 = [“Value1”,“Value2”,“Value3”,“Value1”,“Value2”,“Value3”] )

df2 = DataFrame(Column1 = [“c”,“b”],
Column2 = [8,9],
Column3 = [“Value1”,“Value2”,])

So the array a = [8,9] from column 2 of df2
d = Dict(“c” = [1,3,4,6] , “b” = [2,5]) has as keys c and b and as values the indexes of c and b in the dataframe df1

[quote=“jnewbie, post:3, topic:71382, full:true”]

df1 = DataFrame(Column1 = [“c”,“b”,“c”,“c”,“b”,“c”],
Column2 = [“Value1”,“Value2”,“Value3”,“Value1”,“Value2”,“Value3”],
Column3 = [“Value1”,“Value2”,“Value3”,“Value1”,“Value2”,“Value3”] )

df2 = DataFrame(Column1 = [“c”,“b”],
Column2 = [8,9],
Column3 = [“Value1”,“Value2”,])

So the array a = [8,9] from column 2 of df2
d = Dict(“c” = [1,3,4,6] , “b” = [2,5]) has as keys c and b and as values the indexes of c and b in the dataframe df1

I’m not sure how the DataFrames you posted twice now relate to the original question.

Your original question was how can I go from

julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

to a Dict which has values

julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

the answer to this question is

julia> d["c"] .= 8; d["b"] .= 9; 

julia> d
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [9, 9]

Here I am overwriting the existing arrays stored in the dictionary with the numbers provided. If you don’t want to hardcode those numbers, you have to pick them out from the DataFrame, doing something like

julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

julia> for k ∈ keys(d)
           d[k] .= df2[df2.Column1 .== k, :Column2]
       end

julia> d
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [9, 9]

Thank you it works perfectly !

If it helps anyone, to create a new dictionary without modifying the previous one I did this:

d2 = Dict(k =>(copy(d[k]) .= df2[df2 .== k, :Column2]) for k ∈ keys(d))

Accessors.jl provides a simple way to set specific values in a dictionary without modifying the original dict:

using Accessors

julia> d = Dict("c" => [1,3,4,6], "b" => [2, 5])

julia> d2 = @set d["c"] = fill(8, 4)
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [2, 5]

julia> d
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [1, 3, 4, 6]
  "b" => [2, 5]

You can also keep the "c" vector with the original length and assign its values:

julia> d2 = @set d["c"] |> Elements() = 8
Dict{String, Vector{Int64}} with 2 entries:
  "c" => [8, 8, 8, 8]
  "b" => [2, 5]

This approach is convenient, but less efficient if you need lots of these assignments: the dict gets copied each time.