如何从julia dataframe创建字典?

如何从julia dataframe创建字典?,julia,julia-dataframe,Julia,Julia Dataframe,我有一个类似下面的df,我想从df获取字典 df = DataFrame(id=[1, 2, 3, 4], value=["Rajesh", "John", "Jacob", "sundar"], other=[0.43, 0.42,0.54, 0.63]) │ Row │ id │ value │ other │ │ │ Int64 │ String │ Float64 │ ├─────┼──

我有一个类似下面的df,我想从df获取字典

df = DataFrame(id=[1, 2, 3, 4], value=["Rajesh", "John", "Jacob", "sundar"], other=[0.43, 0.42,0.54, 0.63])

│ Row │ id    │ value  │ other   │
│     │ Int64 │ String │ Float64 │
├─────┼───────┼────────┼─────────┤
│ 1   │ 1     │ Rajesh │ 0.43    │
│ 2   │ 2     │ John   │ 0.42    │
│ 3   │ 3     │ Jacob  │ 0.54    │
│ 4   │ 4     │ sundar │ 0.63    │
预期产出:

{1: 'Rajesh', 2: 'John', 3: 'Jacob', 4: 'sundar'}
我知道如何在熊猫身上做到这一点

df.set_index("id")["value"].to_dict()

在julia中,熊猫的等效代码是什么?

要从数据帧制作字典,您可以编写:

julia> Dict(pairs(eachcol(df)))
Dict{Symbol,AbstractArray{T,1} where T} with 3 entries:
  :value => ["Rajesh", "John", "Jacob", "sundar"]
  :id    => [1, 2, 3, 4]
  :other => [0.43, 0.42, 0.54, 0.63]
但是,您需要的是从向量(恰好存储在数据帧中)生成字典,您可以通过以下方式执行此操作(模式非常相似,但仅适用于向量):

如果您想要从
:id
:value
的映射,请写入(假设汇总
:id
是唯一的;同样-这只是两个向量,它们存储在数据帧中的事实在这里并不重要):


不客气:)。我希望有帮助。重点是熊猫中的许多定制功能只是Julia Base的标准功能。我完全同意你的观点,先生
julia> Dict(pairs(df.value))
Dict{Int64,String} with 4 entries:
  4 => "sundar"
  2 => "John"
  3 => "Jacob"
  1 => "Rajesh"
julia> Dict(Pair.(df.id, df.value))
Dict{Int64,String} with 4 entries:
  4 => "sundar"
  2 => "John"
  3 => "Jacob"
  1 => "Rajesh"