# Dict from dataframe

**URL:** <https://discourse.julialang.org/t/dict-from-dataframe/84277>\
**Category:** General Usage\
**Tags:** dictionary, dataframes\
**Created:** [July 15, 2022, 3:31pm UTC](https://discourse.julialang.org/t/dict-from-dataframe/84277 "2022-07-15T15:31:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 15, 2022, 3:31pm UTC](https://discourse.julialang.org/t/dict-from-dataframe/84277/1 "2022-07-15T15:31:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 15, 2022, 3:51pm UTC](https://discourse.julialang.org/t/dict-from-dataframe/84277/2 "2022-07-15T15:51:00Z")

</div>

```julia
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]

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 15, 2022, 3:52pm UTC](https://discourse.julialang.org/t/dict-from-dataframe/84277/3 "2022-07-15T15:52:17Z")

</div>

Another alternative:

```julia
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]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 15, 2022, 6:56pm UTC](https://discourse.julialang.org/t/dict-from-dataframe/84277/4 "2022-07-15T18:56:50Z")

</div>

And using Julia broadcasting for compactness:

```julia
 Dict(df.ID .=> eachrow([df.X df.Y]))

  4 => [14, 13]
  2 => [12, 11]
  3 => [13, 12]
  1 => [11, 10]

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 15, 2022, 8:20pm UTC](https://discourse.julialang.org/t/dict-from-dataframe/84277/5 "2022-07-15T20:20:12Z")

</div>

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
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

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 15, 2022, 10:34pm UTC](https://discourse.julialang.org/t/dict-from-dataframe/84277/6 "2022-07-15T22:34:22Z")

</div>

```julia
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
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)

```
