# How to construct a dataframe of dataframes

**URL:** <https://discourse.julialang.org/t/how-to-construct-a-dataframe-of-dataframes/27141>\
**Category:** New to Julia\
**Created:** [August 4, 2019, 12:32am UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-of-dataframes/27141 "2019-08-04T00:32:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [August 4, 2019, 12:32am UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-of-dataframes/27141/1 "2019-08-04T00:32:57Z")

</div>

Say, I was trying to create a 3-d Dataframe, but seems like Dataframes can only be 2-d. Someone suggested I create “Dataframe of Dataframes”, where each entry of a Dataframe is itself a dataframe. I was not sure how to do this. I tried a few different syntaxes, but did not seem to work.

`df = DataFrame(DataFrame(rand(4,4), 5)`

This syntax will not work. I basically need a dataframe with 5 entries, where each entry is a 4x4 Dataframe.

I would like to index the resulting Dataframe of Dataframes like  
`df[:col_name][:depth_name][n]`

I saw this [post](https://discourse.julialang.org/t/how-to-to-a-dataframe-of-dataframes/22885) but this looks like an array of Dataframes as opposed to a Dataframe of Dataframes. Now this might work, but just need to understand how to index it.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [August 4, 2019, 12:39am UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-of-dataframes/27141/2 "2019-08-04T00:39:24Z")

</div>

Yeah, you have a few different options here:

Array of DataFrames:

```julia
dfs = DataFrame[]
push!(dfs, DataFrame(rand(4, 4)))
push!(dfs, DataFrame(rand(4, 4)))

df[1] # get 1st DataFrame
df[2] # get 2nd DataFrame

```

If your DataFrames all have the same columns you might do something like:

```julia
df1 = DataFrame(a=[1,2,3], b=[4,5,6])
df2 = DataFrame(a=[7,8,9], b=[10,11,12])

df_all = DataFrame(a=[df1.a, df2.a], b=[df1.b, df2.b])

df_all.a[1] # access df1.a
df_all.a[2] # access df2.a

```

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [August 4, 2019, 12:45am UTC](https://discourse.julialang.org/t/how-to-construct-a-dataframe-of-dataframes/27141/3 "2019-08-04T00:45:17Z")

</div>

Oh I see. So you are saying that the different columns of the outer dataframe are matched to the inner dataframes. That is why `a=[df1.a, df2.a]`. Let me play around with this. Thanks for your help.
