No need to thank me for my understanding, as I still haven’t understood what you’re after
In particular, I don’t see how your output is related to the code you’ve provided - in your code the left hand side of all your assignments is either datas[data1[1]]
or datas[data2[1]]
or datas[data3[1]]
, so in any case the first column of your DataFrames. You therefore can’t end up with the keys 7 or 9 in your Dict, as these numbers only appear in the second columns of df2
.
In any case I now understand that you are trying to append something to your dictionary, whereas your current code overwrites the keys. Consider:
julia> d = Dict(1 => "a")
Dict{Int64, String} with 1 entry:
1 => "a"
julia> d[1] = "b"
"b"
julia> d
Dict{Int64, String} with 1 entry:
1 => "b"
If you want to append, you need to get the item first:
julia> d[1] = d[1] * "c"
"bc"
julia> d
Dict{Int64, String} with 1 entry:
1 => "bc"
But what if the item doesn’t exist yet?
julia> d[2] = d[2] * "d"
ERROR: KeyError: key 2 not found
That’s what get
is for, here you can supply a default value that will be returned if the key doesn’t exist. In your case it’s useful to return an empty string to append to:
julia> d[2] = get(d, 2, "") * "d"
"d"
julia> d
Dict{Int64, String} with 2 entries:
2 => "d"
1 => "bc"
So your code could look something like this:
using DataFrames
df1=DataFrame(p1 = [1, 2, 3], p2 = [1, 2, 3])
df2=DataFrame(q1 = [1, 2, 3], q2 = [3, 7, 9])
df3=DataFrame(r1 = [0, 1], r2 = [1, 1])
datas = Dict()
for data1 in eachrow(df1)
for data2 in eachrow(df2)
for data3 in eachrow(df3)
v1, v2, v3 = data1[1], data2[1], data3[1]
s1, s2, s3 = string.(data1[2], data2[2], data3[2])
if v1 == v2 == v3
datas[v1] = get(datas, v1, "") * s1 * s2 * s3
elseif v1 == v2 != v3
datas[v1] = get(datas, v1, "") * s1 * s2
elseif v1 == v3 != v2
datas[v1] = get(datas, v1, "") * s1 * s2
elseif v2 != v3 != v1
datas[v2] = get(datas, v2, "") * s2 * s3
elseif v1 != v3 != v2
datas[v1] = get(datas, v1, "") * s1
datas[v2] = get(datas, v2, "") * s2
datas[v3] = get(datas, v3, "") * s3
end
end
end
end
This gives a very different result from the one you’re looking for above, but this is mainly because I still don’t understand what you’re actually trying to compare against what.