简体   繁体   中英

convert some rows in rows of a multiindex in pandas dataframe

Lets say I have a pandas dataframe as follows:

     A      B      C      D
0    a0     b0     c0     d0
1    a1     b1     c1     d1
2    a2     b2     c2     d2
3    a3     b3     c3     d3

I would like to know how I can convert it to this.

                   A      B
0    C      c0     a0     b0          
     D      d0     a0     b0          
1    C      c1     a1     b1          
     D      d1     a1     b1          
2    C      c2     a2     b2          
     D      d2     a2     b2          
3    C      c3     a3     b3          
     D      d3     a3     b3          

basically making a few columns as rows and creating a multi index.

Well, melt will pretty much get it in the form you want and then you can set the index as desired:

print df

0  a0  b0  c0  d0
1  a1  b1  c1  d1
2  a2  b2  c2  d2
3  a3  b3  c3  d3

Now use melt to stack (note, I reset the index and use that column as an id_var because it looks like you want the [0,1,2,3] index including in the stacking):

new = pd.melt(df.reset_index(),value_vars=['C','D'],id_vars=['index','A','B'])
print new

  index   A   B variable value
0      0  a0  b0        C    c0
1      1  a1  b1        C    c1
2      2  a2  b2        C    c2
3      3  a3  b3        C    c3
4      0  a0  b0        D    d0
5      1  a1  b1        D    d1
6      2  a2  b2        D    d2
7      3  a3  b3        D    d3

Now just set the index (well sort it and then set the index to make it look like your desired output):

new = new.sort(['index']).set_index(['index','variable','value'])
print new

                       A   B
index variable value        
0     C        c0     a0  b0
      D        d0     a0  b0
1     C        c1     a1  b1
      D        d1     a1  b1
2     C        c2     a2  b2
      D        d2     a2  b2
3     C        c3     a3  b3
      D        d3     a3  b3 

If you don't need the [0,1,2,3] as part of the stack, the melt command is a bit cleaner:

print pd.melt(df,value_vars=['C','D'],id_vars=['A','B'])

    A   B variable value
0  a0  b0        C    c0
1  a1  b1        C    c1
2  a2  b2        C    c2
3  a3  b3        C    c3
4  a0  b0        D    d0
5  a1  b1        D    d1
6  a2  b2        D    d2
7  a3  b3        D    d3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM