title: How to maintain Pandas DataFrame index order when using stack/unstack?
body: <p><strong>Example One:</strong>
  Notice the index order of the given Pandas DataFrame <code>df</code>:</p>
  
  <pre><code>&gt;&gt;&gt; df
                A  B
  first second      
  zzz   z       2  4
        a       1  5
  aaa   z       6  3
        a       7  8
  </code></pre>
  
  <p>After using the <code>stack</code> and <code>unstack</code> methods on the given <code>df</code> DataFrame object, the index is automatically sorted lexicographically (alphabetically) so that one loses the original order of the rows.</p>
  
  <pre><code>&gt;&gt;&gt; df.unstack().stack()
                A  B
  first second      
  aaa   a       7  8
        z       6  3
  zzz   a       1  5
        z       2  4
  </code></pre>
  
  <p>Is it possible to maintain the original ordering after the <code>unstack/stack</code> operations above?</p>
  
  <p>According to official documentation <a href="http://pandas.pydata.org/pandas-docs/stable/reshaping.html#reshaping-by-stacking-and-unstacking" rel="noreferrer">reshaping-by-stacking-and-unstacking</a>:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p><strong>Example Two:</strong></p>
  
  <pre><code>&gt;&gt;&gt; dfu = df.unstack()
  &gt;&gt;&gt; dfu
           A      Z   
  second   a  z   a  z
  first               
  aaa      7  6   8  3
  zzz      1  2   5  4
  </code></pre>
  
  <p>If the original index is perserved we need <code>dfu</code> like so:</p>
  
  <pre><code>&gt;&gt;&gt; dfu
               A      Z   
      second   a  z   a  z
      first               
      zzz      1  2   5  4
      aaa      7  6   8  3
  </code></pre>
  
  <p>What I'm looking for is a solution that could be used to revert the index order based on the original dataframe after an <code>unstack()</code> or <code>stack()</code> method has been called.</p>