Extracting column labels of the cells meeting a given condition
<p>Suppose the data at hand is in the following form:</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'A':[1,10,20], 'B':[4,40,50], 'C':[10,11,12]})
</code></pre>

<p>I can compute the minimum by row with:</p>

<pre><code>df.min(axis=1)
</code></pre>

<p>which returns <code>1 10 12</code>.</p>

<p>Instead of the values, I would like to create a pandas Series containing the column labels of the corresponding cells.</p>

<p>That is, I would like to get <code>A A C</code>.</p>

<p>Thanks for any suggestions. </p>

<p>you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.idxmin.html" rel="nofollow">idxmin(axis=1)</a> method:</p>

<pre><code>In [8]: df.min(axis=1)
Out[8]:
0     1
1    10
2    12
dtype: int64

In [9]: df.idxmin(axis=1)
Out[9]:
0    A
1    A
2    C
dtype: object

In [11]: df.idxmin(axis=1).values
Out[11]: array(['A', 'A', 'C'], dtype=object)
</code></pre>

python pandas dataframe