# How to to a DataFrame of DataFrames

**URL:** <https://discourse.julialang.org/t/how-to-to-a-dataframe-of-dataframes/22885>\
**Category:** General Usage\
**Created:** [April 7, 2019, 6:32pm UTC](https://discourse.julialang.org/t/how-to-to-a-dataframe-of-dataframes/22885 "2019-04-07T18:32:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steph\_de\_paris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steph_de_paris/32/7780_2.png) [@steph\_de\_paris](https://discourse.julialang.org/u/steph_de_paris)\
**Post date:** [April 7, 2019, 6:32pm UTC](https://discourse.julialang.org/t/how-to-to-a-dataframe-of-dataframes/22885/1 "2019-04-07T18:32:08Z")

</div>

I need to to a DataFramce of DataFrames. When I use

```julia
dh=[DataFrame(a=1,b=1);DataFrame(a=1,b=1)] 

```

it is OK since dh is a DataFrame. Indeed, I obtain this DataFrame:

2 rows × 2 columns  
a b  
Int64 Int64  
1 1 1  
2 1 1

Nevertheless, when I use:

```julia
dg=DataFrame()
dg[1]=DataFrame(a=1,b=1)
dg[2]=DataFrame(a=1,b=1)
df=[dg[1];dg[2]]

```

I obtain a structure which is NOT a DataFrame but 2 arrays. Indeed, I obtain:

2-element Array{DataFrame,1}:  
1×2 DataFrame  
Row a b  
Int64 Int64  
1 1 1  
1×2 DataFrame  
Row a b  
Int64 Int64  
1 1 1

Could you tell me how to obtain a DataFrame df identical to dh BUT by using df=[dg[1];dg[2]] ?

Thanks !

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 13, 2019, 10:03pm UTC](https://discourse.julialang.org/t/how-to-to-a-dataframe-of-dataframes/22885/2 "2019-04-13T22:03:55Z")

</div>

`[dg[1][1];dg[2][1]]` will do the trick. That’s because `dg[1]` is a vector of `DataFrame` objects: since data frame columns must always be vectors, `dg[1]=DataFrame(a=1,b=1)` automatically wraps the RHS in a vector. But you probably shouldn’t do that in general. Can you give more details about what you’re trying to do?

---

<div class="post-metadata">

**Author:** ![steph\_de\_paris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steph_de_paris/32/7780_2.png) [@steph\_de\_paris](https://discourse.julialang.org/u/steph_de_paris)\
**Post date:** [April 14, 2019, 11:28am UTC](https://discourse.julialang.org/t/how-to-to-a-dataframe-of-dataframes/22885/3 "2019-04-14T11:28:28Z")

</div>

Thanks very much ! In fact, I need to construct a DataFrame with many columns whose number q is a parameter. I found this solution:

```julia
df=DataFrame([Float64 for k in 1:q],[Symbol("code_$k") for k in 1:q],0);

```

And to populate df with for example 10 rows of data, I use:

```julia
        for k in 1:q
            for i in 1:10
                data[i,k]=i*k;
            end
        end

        for i in 1:10
            pop=[
                data[i,:]
               ]
            push!(df,pop)
        end

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 14, 2019, 12:08pm UTC](https://discourse.julialang.org/t/how-to-to-a-dataframe-of-dataframes/22885/4 "2019-04-14T12:08:58Z")

</div>

Something like this will be much more efficient:

```julia
df=DataFrame(AbstractVector[data[:, k] for k in 1:q], [Symbol("code_$k") for k in 1:q]);

```
