Pandas – convert colums to rows in multiindex dataframe

Question:

How can I convert the following pandas dataframe:

                dw1 dw2
db_c    c_dat   a   e
        c_idx   b   f
db_s    s_dat   c   g
        s_idx   d   h

to this:

db_c    c_dat   dw1     a
        c_dat   dw2     e
        c_idx   dw1     b
        c_idx   dw2     f
db_s    s_dat   dw1     c
        s_dat   dw2     g
        s_idx   dw1     d
        s_idx   dw2     h

The count of the columns in the first dataframe is variable.

Asked By: cogimi

||

Answers:

You need to stack as Series, the columns will automatically form a new index level:

out = df.stack()

Output:

db_c  c_dat  dw1    a
             dw2    e
      c_idx  dw1    b
             dw2    f
db_s  s_dat  dw1    c
             dw2    g
      s_idx  dw1    d
             dw2    h
dtype: object

If you want a DataFrame as output, further reset_index:

out = df.stack().to_frame(name='col')

               col
db_c c_dat dw1   a
           dw2   e
     c_idx dw1   b
           dw2   f
db_s s_dat dw1   c
           dw2   g
     s_idx dw1   d
           dw2   h
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.