# What is easiest way to transform an array of objects into a data table?

**URL:** https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310
**Category:** General Usage
**Tags:** question
**Created:** [September 21, 2018, 7:36pm UTC](https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310 "2018-09-21T19:36:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [September 21, 2018, 7:36pm UTC](https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310/1 "2018-09-21T19:36:30Z")

</div>

Let’s say you have a struct Foo, that has 30 or so named fields:

```julia
struct Foo
  a::Int  
  b::AbstractFloat 
  ...
  y::Bool 
  z::Int 
end

```

Now you want to store it (without the use of JLD).

What is the go-to method for transforming a vector of these Foo objects into a table?

* * *

I’ve tried `DataFrames`, `DataTables`, `DataData`, `DataLife`, `DataTaxes`, etc.

But it seems like this extremely simple use-case is never spelled out in laymanese

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [September 21, 2018, 7:48pm UTC](https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310/2 "2018-09-21T19:48:17Z")

</div>

Could you expand? Do you want each Foo object to be a column in a DataFrame, with each row a field, or do you want a vector of Foo objects?

---

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [September 21, 2018, 7:49pm UTC](https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310/3 "2018-09-21T19:49:17Z")

</div>

I would like to have:

- each Foo object in the array as a row in the database
- with the columns as the fields (that are assumed to have a fixed-byte representation)

* * *

**edit:** here’s a quick rundown of how it would look:

| index | a | b | … | y | z |
| --- | --- | --- | --- | --- | --- |
| 1 | 2 | 3.1 | … | true | 1 |
| 2 | 52 | 0.4 | … | false | 2 |
| … | … | … | … | … | … |
| 98 | 22 | 1.5 | … | false | 2 |
| 99 | -1 | 5.7 | … | false | 1 |

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [September 21, 2018, 7:54pm UTC](https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310/4 "2018-09-21T19:54:05Z")

</div>

Does this work?

```julia
function makeVec(x::Foo)
    t = fieldnames(typeof(x))
    [getproperty(x, field) for field in t]
end

df = DataFrame(a = Int[], b = Int[]... allocate the types and names)

for x in VectorOfFoos
    push!(df, makeVec(x)
end

```

You could also do

```julia
df = DataFrame(a = [x.a for x in VectorOfFoos], b = [x.b for x in VectorOfFoos]...)

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 21, 2018, 7:56pm UTC](https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310/5 "2018-09-21T19:56:55Z")

</div>

Using (the yet unreleased StructArrays and the latest DataFrames) something like this is possible

```julia
julia> using DataFrames, StructArrays

julia> struct Foo
         a::Int
         b::Float64
       end

julia> c = [Foo(1, 2.0), Foo(2, 4.0)]
2-element Array{Foo,1}:
 Foo(1, 2.0)
 Foo(2, 4.0)

julia> DataFrame(StructArray(c))
2×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Float64 │
├─────┼───────┼─────────┤
│ 1 │ 1 │ 2.0 │
│ 2 │ 2 │ 4.0 │

```

Edit: Updated the answer since master DataFrames makes this easier.

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [September 22, 2018, 1:01am UTC](https://discourse.julialang.org/t/what-is-easiest-way-to-transform-an-array-of-objects-into-a-data-table/15310/6 "2018-09-22T01:01:39Z")

</div>

for shorter structs you might also be able do something like  
`reduce(vcat, map(x-> [x.a x.b], arrFoo))`

Add: There might be a performance regression using `mapreduce()`

```julia
struct n; a; b; end;
p = fill(n(rand(1:10), rand()*10), 10^6);
@time mapreduce(x->[x.a x.b], vcat, p);
5.99 seconds
@time reduce(vcat, map(x-> [x.a x.b], p));
~0.85 sec
```
