# DataFrame to Dict via Vector of Nested Named Tuples

**URL:** https://discourse.julialang.org/t/dataframe-to-dict-via-vector-of-nested-named-tuples/72214
**Category:** New to Julia
**Tags:** jump, dataframes, namedtuple
**Created:** [November 28, 2021, 9:56pm UTC](https://discourse.julialang.org/t/dataframe-to-dict-via-vector-of-nested-named-tuples/72214 "2021-11-28T21:56:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Rob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rob/32/52425_2.png) [@Rob](https://discourse.julialang.org/u/Rob)
#### Post date: [November 28, 2021, 9:56pm UTC](https://discourse.julialang.org/t/dataframe-to-dict-via-vector-of-nested-named-tuples/72214/1 "2021-11-28T21:56:54Z")

</div>

I am building a JuMP transportation model, and looking for an alternate approach to create a dict from a dataframe. Here is my starting (simplified) DataFrame:

```julia
using DataFrames
using NamedTupleTools
df = DataFrame(City=["Boston", "Dallas", "Chicago"], State=["MA", "TX", "IL"], Part = [:X, :Y, :Z])

3×3 DataFrame
 Row │ City State Part   
     │ String String Symbol 
─────┼─────────────────────────
   1 │ Boston MA X      
   2 │ Dallas TX Y      
   3 │ Chicago IL Z  

```

And I create this vector of struct (to serve as the key for a dict of customer demand data):

```julia
3-element Vector{Demand}:
 Demand(Loc("Boston", "MA"), :X)
 Demand(Loc("Dallas", "TX"), :Y)
 Demand(Loc("Chicago", "IL"), :Z)

```

Via these steps:

```julia
struct Loc
    City::String
    State::String
end

# Loc as a named tuple
loc_nt = Tables.rowtable(DataFrames.select(df, :City, :State))
# convert to a structure
loc = structfromnt.(Loc, loc_nt)

struct Demand
    Dest::Loc
    Part::Symbol
end

dmd_st = Demand[]
for i in 1 : nrow(df)
    push!(dmd_st, Demand(loc[i], df.Part[i]))
end

```

My question: is there a better way to do this, perhaps using nested named tuples and converting them to the struc? Coming from R, it’s hard to get used to loops. I want to keep the nested struct to simplify some constraints.

Thanks for any suggestions.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [November 28, 2021, 10:17pm UTC](https://discourse.julialang.org/t/dataframe-to-dict-via-vector-of-nested-named-tuples/72214/2 "2021-11-28T22:17:54Z")

</div>

I would just do:

```julia
julia> Demand.(Loc.(df.City, df.State), df.Part)
3-element Vector{Demand}:
 Demand(Loc("Boston", "MA"), :X)
 Demand(Loc("Dallas", "TX"), :Y)
 Demand(Loc("Chicago", "IL"), :Z)

```

and since you are coming from R an alternative is:

```julia
julia> using DataFramesMeta

julia> @with(df, Demand.(Loc.(:City, :State), :Part))
3-element Vector{Demand}:
 Demand(Loc("Boston", "MA"), :X)
 Demand(Loc("Dallas", "TX"), :Y)
 Demand(Loc("Chicago", "IL"), :Z)

```

---

<div class="post-metadata">

### Author: ![Rob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rob/32/52425_2.png) [@Rob](https://discourse.julialang.org/u/Rob)
#### Post date: [November 28, 2021, 11:17pm UTC](https://discourse.julialang.org/t/dataframe-to-dict-via-vector-of-nested-named-tuples/72214/3 "2021-11-28T23:17:17Z")

</div>

Fantastic! I knew I was overcomplicating it. Thank you so much.
