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>Here's another way.</p>

<pre><code>In [5]: df1 = df.rank(1).astype(int)

In [6]: df3 = df1.replace({rank: name for rank, name in enumerate(df1.columns, 1)})

In [7]: df3.columns = range(1, 1 + df3.shape[1])

In [8]: df3
Out[8]: 
   1  2  3  4  5
0  B  A  C  E  D
1  A  E  D  C  B
2  E  B  C  D  A
3  B  C  D  E  A
</code></pre>

<p>Yet another way.</p>

<pre><code>In [6]: ranks = df.rank(axis=1).astype(int)-1
In [7]: new_values = df.columns.values.take(ranks)

In [8]: pd.DataFrame(new_values)
Out[8]: 
   0  1  2  3  4
0  B  A  C  E  D
1  A  E  D  C  B
2  E  B  C  D  A
3  B  C  D  E  A
</code></pre>

python pandas numpy