python - Replace DataFrame column index with MultiIndex -


this question has answer here:

i have pandas dataframe "flat" column index, ['sample 1', 'sample 2' ...], , i've constructed multiindex has 2 levels, first of having same elements dataframe's column index ('sample 1' ...). want replace column index of dataframe multiindex, each of elements of multiindex replacing column index element has matching name. so, column headed 'sample 1' should headed ('sample 1', 'group x'), column heading 'sample 2' should ('sample 2', 'group x'), , forth.

for example, dataframe might like:

            b    c row1     1    2    3 row2     4    5    6   : rown     7    8    9 

and multiindex [('a', 'g1'), ('b', 'g1'), ('c', 'g2')]

with end result looking like:

        g1   g1   g2             b    c row1     1    2    3 row2     4    5    6   : rown     7    8    9 

seems should simple join or matching concatenation, can't find method (and/or keywords) it.

just assign columns instance attribute

in [10]: df = dataframe(randn(10, 3), columns=list('abc'))  in [11]: df.columns = [list('abc'), df.columns]  in [12]: df out[12]:             b      c             b      c 0  2.320 -1.670  0.511 1  0.350 -1.877  0.627 2  1.671  0.347  1.328 3  0.896 -0.061  0.083 4  1.168  1.212 -1.127 5 -0.015  0.402  0.249 6 -0.576  0.898 -0.464 7 -0.939  0.198 -2.698 8 -0.767  0.532 -0.369 9  0.365  0.293 -0.676  in [13]: df.columns out[13]: multiindex [(u'a', u'a'), (u'b', u'b'), (u'c', u'c')] 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -