Dictionary 如何在julia中更改字典的键?
我想用dataframe列替换字典中一个键的值。而如果第一个键中的任何值重复,则第二个键中的值将重复 例如:Dictionary 如何在julia中更改字典的键?,dictionary,julia,Dictionary,Julia,我想用dataframe列替换字典中一个键的值。而如果第一个键中的任何值重复,则第二个键中的值将重复 例如: using DataFrames d = d = Dict(:1 => 2, :2=>3, :3=>4) df = DataFrame(a = [1,1,2,2,3,3], b = [7,6,5,4,3,2], c = [9,8,7,6,6,8]) #Output Dict{Int64,Int64} wi
using DataFrames
d = d = Dict(:1 => 2, :2=>3, :3=>4)
df = DataFrame(a = [1,1,2,2,3,3],
b = [7,6,5,4,3,2],
c = [9,8,7,6,6,8])
#Output
Dict{Int64,Int64} with 4 entries:
3 => 4
2 => 7
1 => 6
#Dataframe
8 rows × 3 columns
a b c
Int64 Int64 Int64
1 7 9
1 6 8
2 5 7
2 4 6
3 3 6
3 2 8
我想用df.a
替换Dict=d
的键,以生成以下词典
Dict{Int64,Int64} with 6 entries:
1 => 6
1 => 6
2 => 7
2 => 7
3 => 4
3 => 4
我可以知道如何实现这个操作吗
更新
首先,感谢@gTcV的建议和帮助,非常感谢
只需提取这些值,就可以按照@gTcV提到的上述帖子进行操作。
但是,要实现上述数据帧操作,可以使用以下代码段:
using DataFrames
d = d = Dict(:1 => 2, :2=>3, :3=>4)
df = DataFrame(a = [1,1,2,2,3,3],
b = [7,6,5,4,3,2],
c = [9,8,7,6,6,8])
df[!, c] = [i=d[i] for i in df[:, :a]]
Output: -->
6×3 DataFrame
Row │ a b c
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 1 7 2
2 │ 1 6 2
3 │ 2 5 3
4 │ 2 4 3
5 │ 3 3 4
6 │ 3 2 4
谢谢
Dicts
不允许重复按键,因此您所要求的是不可能的。如果只是关于输出,您可以执行以下操作:
for a in df[:,1]
println("$a => $(d[a])")
end
Dict
s不允许重复按键。您需要为输出选择另一个容器。Vector{Pair}
do吗?@gTcV感谢您的响应,是否可以只提取预期输出中显示的值?因为我的目标是只提取值,并确保所有相似键的值保持不变;println(“$a=>$(d[a])”;结束@gTcV感谢您解决了这个问题,非常感谢您的帮助@gTcV您可能会根据您的成功评论为这篇文章写一个答案,然后进行验证?