# Convert NamedTuple with matrix to DataFrame

**URL:** <https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [March 9, 2024, 3:13pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399 "2024-03-09T15:13:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 9, 2024, 3:13pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/1 "2024-03-09T15:13:45Z")

</div>

Hello,

I was wondering if there is a simple way to convert a NamedTuple with a mix of vectors and matrices, such as,

`np = (x = [1 1; 1 2; 2 1; 2 2], y = [1,2,3,4])`

to a DataFrame, such as

`df = DataFrame(x1 = [1,1,2,2], x2=[1,2,1,2], y=[1,2,3,4])`

```julia
4×3 DataFrame
 Row │ x1 x2 y     
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 1 1 1
   2 │ 1 2 2
   3 │ 2 1 3
   4 │ 2 2 4

```

Thank you.

---

<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:** [March 9, 2024, 3:47pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/2 "2024-03-09T15:47:38Z")

</div>

FWIW:

```julia
DataFrame(reduce(vcat, eachcol(x) for x in np), [:x1, :x2, :y])

```

---

<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:** [March 9, 2024, 7:47pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/3 "2024-03-09T19:47:08Z")

</div>

```julia
hcat(DataFrame(np.x,:auto),DataFrame(;np.y))

```

```julia
DataFrame(hcat(np.x,np.y),:auto)

```

```julia
DataFrame(hcat(np...),:auto)

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 9, 2024, 8:46pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/4 "2024-03-09T20:46:11Z")

</div>

```julia
DataFrame(["$k$i" => c for (k,v) in pairs(np) for (i, c) in pairs(eachcol(v))])

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 9, 2024, 8:49pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/5 "2024-03-09T20:49:45Z")

</div>

Thank you everyone for the suggestions. I think the one by sijo is the closest to what I am aiming for. I think the only drawback is the extra “1” added to y. I’ll see if I can find a way to add numbers to columns of matrices.

---

<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:** [March 9, 2024, 9:24pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/6 "2024-03-09T21:24:58Z")

</div>

> [@Christopher\_Fisher](#):
>
> think the only drawback is the extra “1” added to y.

You could tweak it further:

```julia
DataFrame([(k == :x ? "$k$i" : "$k") => c for (k,v) in pairs(np) for (i, c) in pairs(eachcol(v))])

```

---

<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:** [March 9, 2024, 9:27pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/7 "2024-03-09T21:27:15Z")

</div>

I assume you have a more general situation like the following.  
You could adapt the script like this

```julia
julia> np = (x = [1 1; 1 2; 2 1; 2 2],z = [1 1; 1 2; 2 1; 2 2], y = [1,2,3,4])
(x = [1 1; 1 2; 2 1; 2 2], z = [1 1; 1 2; 2 1; 2 2], y = [1, 2, 3, 4])

julia> DataFrame([v isa Matrix ? "$k$i" => c : "$k" => c for (k,v) in pairs(np) for (i, c) in pairs(eachcol(v))])
4×5 DataFrame
 Row │ x1 x2 z1 z2 y     
     │ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
   1 │ 1 1 1 1 1
   2 │ 1 2 1 2 2
   3 │ 2 1 2 1 3
   4 │ 2 2 2 2 4

```

```julia
julia> DataFrame(["$k" => c for (k,v) in pairs(np) for c in eachcol(v)],makeunique=true)    
4×5 DataFrame
 Row │ x x_1 z z_1 y     
     │ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
   1 │ 1 1 1 1 1
   2 │ 1 2 1 2 2
   3 │ 2 1 2 1 3
   4 │ 2 2 2 2 4

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 9, 2024, 11:09pm UTC](https://discourse.julialang.org/t/convert-namedtuple-with-matrix-to-dataframe/111399/8 "2024-03-09T23:09:05Z")

</div>

Another method to tweak rafael’s answer which doesn’t add `1` to `y`:

```julia
DataFrame(["$k"*"$i"^(v!=c)=>c for (k,v) in pairs(np) for
  (i,c) in pairs(eachcol(v))])

```
