# Destructuring object into DataFrame

**URL:** https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964
**Category:** General Usage
**Tags:** dataframes
**Created:** [March 16, 2022, 12:24pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964 "2022-03-16T12:24:04Z")
**Posts on this page:** 7
**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 16, 2022, 12:24pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964/1 "2022-03-16T12:24:04Z")

</div>

Hi all,

I have an array of objects that I would like to convert to a DataFrame. Each object has some arrays and some individual values. I would like to destructure the arrays into individual rows and columns and repeat the individual values while maintaining their types. Here is a sample example:

```julia
using DataFrames

mutable struct M 
    id::Int
    vals::Array{<:Number,2}
end

ms = [M(i, rand(2,2)) for i in 1:2]
df = DataFrame(ms)

```

**output**

```julia
 Row │ id vals                              
     │ Int64 Array…                            
─────┼──────────────────────────────────────────
   1 │ 1 [0.618182 0.00825483; 0.646895 0…
   2 │ 2 [0.450556 0.462105; 0.889429 0.8…

```

After exploring various approaches this is the closest solution:

```julia
rows = mapreduce(x -> [fill(x.id, size(x.vals,1)) x.vals], vcat, ms)
new_df = DataFrame(rows, :auto)

```

**output**

```julia

4×3 DataFrame
 Row │ x1 x2 x3         
     │ Float64 Float64 Float64    
─────┼───────────────────────────────
   1 │ 1.0 0.618182 0.00825483
   2 │ 1.0 0.646895 0.0341765
   3 │ 2.0 0.450556 0.462105
   4 │ 2.0 0.889429 0.843017

```

The main problem is that x1 is not a row of Int. Is there a better/less hacky way to achieve this goal?

I should also note that I do not know the width of the array `vals`. So I cannot hard code column types. However, they will be the same width for each object.

---

<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: [March 16, 2022, 12:59pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964/2 "2022-03-16T12:59:06Z")

</div>

```julia
flatten(combine(df, :id, :vals => ByRow(collect∘eachcol) => AsTable), Not(:id))

```

or if you know `:id` is unique:

```julia
combine(groupby(df, :id), :vals => only => AsTable)

```

---

<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 16, 2022, 1:48pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964/3 "2022-03-16T13:48:40Z")

</div>

> [@bkamins](#):
>
> `combine(groupby(df, :id), :vals => only => AsTable)`

Thank you so much! I knew there was a better solution. I just didn’t know how to find it.

---

<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 16, 2022, 2:53pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964/4 "2022-03-16T14:53:49Z")

</div>

Sorry to bump this. I’ve been struggling to extend this to my use case. I have a vector that I would like to add to a column with a name. I receive an error about an unrecognized column selector. Here is a MWE:

```julia
using DataFrames

mutable struct M 
    id::Int
    vals::Array{<:Number,2}
    vect::Vector{<:Number}
end

ms = [M(i, rand(2,2), rand(2)) for i in 1:2]
df = DataFrame(ms)
combine(
    groupby(df, :id), 
    :vals => only => AsTable, 
    :vect => only => AsTable => :vect
)

```

How can I fix this error? Thanks!

---

<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: [March 16, 2022, 3:39pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964/5 "2022-03-16T15:39:07Z")

</div>

```julia
combine(                      
    groupby(df, :id),         
    :vals => only => AsTable, 
    :vect => only => :vect    
)                             

```

`AsTable` is only needed for auto-generation of multiple column names. You could write e.g.:

```julia
combine(                             
    groupby(df, :id),                
    :vals => only => [:mat1, :mat2], 
    :vect => only => :vect           
)                                    

```

to give names to your marix columns

---

<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 16, 2022, 3:42pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964/6 "2022-03-16T15:42:07Z")

</div>

I did not realize that about AsTable. This actually makes things a lot simpler because I do know the column names. Thanks again!

---

<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: [March 16, 2022, 3:44pm UTC](https://discourse.julialang.org/t/destructuring-object-into-dataframe/77964/7 "2022-03-16T15:44:20Z")

</div>

`AsTable` is mostly for unnesting e.g. `NamedTuple`s which already have column names that you want to retain, see [Nesting and unnesting columns in DataFrames.jl | Blog by Bogumił Kamiński](https://bkamins.github.io/julialang/2022/03/11/unnesting.html).
