Dataframe with two groups of cols. [RESOLVED]

Paulo da Silva p_s_d_a_s_i_l_v_a_ns at netcabo.pt
Fri Jun 14 13:31:26 EDT 2019


Às 04:56 de 14/06/19, Paulo da Silva escreveu:
> Hi!
> 
> How do I create a pandas dataframe with two (or more) groups of cols.?
> 
> Ex.:
> 
> G1       G2
> C1 C2 C3 C1 C2 C3
> Rows of values ...
> 
> I then should be able to access for example
> df['G2']['C3'][<some rows selection>]
> 
> 
> Thanks.
> 

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]:



More information about the Python-list mailing list