Re: 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]: > G1G2 > 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]: > 010 > 110 > 210 > 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]: G1G2G3 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]: -- https://mail.python.org/mailman/listinfo/python-list
Re: Dataframe with two groups of cols. [RESOLVED]
À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'][] > > > 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]: G1G2 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]: 010 110 210 Name: C1, dtype: int64 In [26]: -- https://mail.python.org/mailman/listinfo/python-list
Dataframe with two groups of cols.
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'][] Thanks. -- https://mail.python.org/mailman/listinfo/python-list