twist dataframe by rank
<p>consider the dataframe <code>df</code></p>

<pre><code>np.random.seed([3,1415])
df = pd.DataFrame(np.random.rand(4, 5), columns=list('ABCDE'))
df
</code></pre>

<p><a href="http://i.stack.imgur.com/VuXYh.png" rel="nofollow"><img src="http://i.stack.imgur.com/VuXYh.png" alt="enter image description here"></a></p>

<hr>

<p>I want a dataframe where the columns are ranks and each row is <code>['A', 'B', 'C', 'D', 'E']</code> in rank order.</p>

<p><strong><em>ranks</em></strong>  </p>

<pre><code>df.rank(1).astype(int)
</code></pre>

<p><a href="http://i.stack.imgur.com/VkC62.png" rel="nofollow"><img src="http://i.stack.imgur.com/VkC62.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>expected results</em></strong></p>

<p><a href="http://i.stack.imgur.com/UtYAi.png" rel="nofollow"><img src="http://i.stack.imgur.com/UtYAi.png" alt="enter image description here"></a></p>

<p>Use <code>stack</code>, <code>reset_index</code>, and <code>pivot</code></p>

<pre><code>df.rank(1).astype(int).stack().reset_index() \
  .pivot('level_0', 0, 'level_1').rename_axis(None)
</code></pre>

<p><a href="http://i.stack.imgur.com/plUw1.png" rel="nofollow"><img src="http://i.stack.imgur.com/plUw1.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>Timing</em></strong></p>

<p><a href="http://i.stack.imgur.com/l3FFp.png" rel="nofollow"><img src="http://i.stack.imgur.com/l3FFp.png" alt="enter image description here"></a></p>

python pandas numpy