简体   繁体   中英

How to maintain Pandas DataFrame index order when using stack/unstack?

Example One: Notice the index order of the given Pandas DataFrame df :

>>> df
              A  B
first second      
zzz   z       2  4
      a       1  5
aaa   z       6  3
      a       7  8

After using the stack and unstack methods on the given df DataFrame object, the index is automatically sorted lexicographically (alphabetically) so that one loses the original order of the rows.

>>> df.unstack().stack()
              A  B
first second      
aaa   a       7  8
      z       6  3
zzz   a       1  5
      z       2  4

Is it possible to maintain the original ordering after the unstack/stack operations above?

According to official documentation reshaping-by-stacking-and-unstacking :

Notice that the stack and unstack methods implicitly sort the index levels involved. Hence a call to stack and then unstack, or viceversa, will result in a sorted copy of the original DataFrame or Series

Example Two:

>>> dfu = df.unstack()
>>> dfu
         A      Z   
second   a  z   a  z
first               
aaa      7  6   8  3
zzz      1  2   5  4

If the original index is perserved we need dfu like so:

>>> dfu
             A      Z   
    second   a  z   a  z
    first               
    zzz      1  2   5  4
    aaa      7  6   8  3

What I'm looking for is a solution that could be used to revert the index order based on the original dataframe after an unstack() or stack() method has been called.

You can keep a copy of the original index and reindex to that, thanks Andy Hayden.

Demo:

#              A  B
#first second      
#zzz   z       2  4
#      a       1  5
#aaa   z       6  3
#      a       7  8

print df.index
#MultiIndex(levels=[[u'aaa', u'zzz'], [u'a', u'z']],
#           labels=[[1, 1, 0, 0], [1, 0, 1, 0]],
#           names=[u'first', u'second'])

#set index to variable
index = df.index

#stack and unstack
df = df.unstack().stack()
print df
#              A  B
#first second      
#aaa   a       7  8
#      z       6  3
#zzz   a       1  5
#      z       2  4
#              A  B

df = df.reindex(index)
print df
#              A  B
#first second      
#zzz   z       2  4
#      a       1  5
#aaa   z       6  3
#      a       7  8

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