git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Dataframe with two groups of cols. [RESOLVED]


?s 18:31 de 14/06/19, Paulo da Silva escreveu:
> ?s 04:56 de 14/06/19, Paulo da Silva escreveu:
...


> 
> After digging a lot :-) , and for those who may be interested, I found
> one way:
> 
> In [21]: d1 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8,
> 9]]),columns=['C1', 'C2', 'C3'])
> 
> In [22]: d2 = pd.DataFrame(np.array([[10, 2, 3], [10, 5, 6], [10, 8,
> 9]]),columns=['C1', 'C2', 'C3'])
> 
> In [23]: d=pd.concat([d1,d2],keys=['G1','G2'],axis=1)
> 
> In [24]: d
> Out[24]:
>   G1        G2
>   C1 C2 C3  C1 C2 C3
> 0  1  2  3  10  2  3
> 1  4  5  6  10  5  6
> 2  7  8  9  10  8  9
> 
> In [25]: d['G2']['C1']
> Out[25]:
> 0    10
> 1    10
> 2    10
> Name: C1, dtype: int64
> 
> In [26]:
> 

And I noticed that things are yet more flexible ...
For ex. we can add further data

In [12]: d['G3','C1']=['v1','v2','v3']

In [13]: d
Out[13]:
  G1        G2        G3
  C1 C2 C3  C1 C2 C3  C1
0  1  2  3  10  2  3  v1
1  4  5  6  10  5  6  v2
2  7  8  9  10  8  9  v3

... but starting with an empty dataframe does not work!
In [3]: df=pd.DataFrame()

In [4]: df['G1','c1']=[1,2,3]

In [5]: df
Out[5]:
   (G1, c1)
0         1
1         2
2         3

In [6]: