Getting n-th ranked column IDs per row of a dataframe - Python/Pandas
<p>I am trying to find a method for finding the nth ranked value and returning the column name. So for example, given a data-frame: </p>

<pre><code>df = pd.DataFrame(np.random.randn(5, 4), columns = list('ABCD'))

# Return column name of "MAX" value, compared to other columns in any particular row.

df['MAX1_NAMES'] = df.idxmax(axis=1)

print df

          A         B         C         D MAX1_NAMES
0 -0.728424 -0.764682 -1.506795  0.722246          D
1  1.305500 -1.191558  0.068829 -1.244659          A
2 -0.175834 -0.140273  1.117114  0.817358          C
3 -0.255825 -1.534035 -0.591206 -0.352594          A
4 -2.408806 -1.925055 -1.797020  2.381936          D
</code></pre>

<p>This would find the highest value in the rows and return the column name where it occurred. But I need the case where I can choose the particular rank of the desired value, and hopefully get a data frame like the following:</p>

<pre><code>          A         B         C         D MAX1_NAMES  MAX2_NAMES
0 -0.728424 -0.764682 -1.506795  0.722246          D           A
1  1.305500 -1.191558  0.068829 -1.244659          A           C
2 -0.175834 -0.140273  1.117114  0.817358          C           D
3 -0.255825 -1.534035 -0.591206 -0.352594          A           D
4 -2.408806 -1.925055 -1.797020  2.381936          D           C
</code></pre>

<p>Where <code>MAX2_NAMES</code> is the second largest value in the row. </p>

<p>Thanks.</p>