How to to a DataFrame of DataFrames

#1

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

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:

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 !

0 Likes

#2

[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?

0 Likes

#3

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

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:

        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
0 Likes

#4

Something like this will be much more efficient:

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