Select columns from a DataFrame based on values in a row in pandas
<p>Say I have the same dataframe from <a href="http://stackoverflow.com/questions/25479607/pandas-min-of-selected-row-and-columns">this question</a>:</p>

<pre><code>    A0      A1      A2      B0      B1      B2      C0      C1
0   0.84    0.47    0.55    0.46    0.76    0.42    0.24    0.75
1   0.43    0.47    0.93    0.39    0.58    0.83    0.35    0.39
2   0.12    0.17    0.35    0.00    0.19    0.22    0.93    0.73
3   0.95    0.56    0.84    0.74    0.52    0.51    0.28    0.03
4   0.73    0.19    0.88    0.51    0.73    0.69    0.74    0.61
5   0.18    0.46    0.62    0.84    0.68    0.17    0.02    0.53
6   0.38    0.55    0.80    0.87    0.01    0.88    0.56    0.72
</code></pre>

<p>But instead of wanting to return the minimum value of each row (of only B0, B1, B2)</p>

<pre><code>    A0      A1      A2      B0      B1      B2      C0      C1      Minimum
0   0.84    0.47    0.55    0.46    0.76    0.42    0.24    0.75    0.42
1   0.43    0.47    0.93    0.39    0.58    0.83    0.35    0.39    0.39
2   0.12    0.17    0.35    0.00    0.19    0.22    0.93    0.73    0.00
3   0.95    0.56    0.84    0.74    0.52    0.51    0.28    0.03    0.51
4   0.73    0.19    0.88    0.51    0.73    0.69    0.74    0.61    0.51
5   0.18    0.46    0.62    0.84    0.68    0.17    0.02    0.53    0.17
6   0.38    0.55    0.80    0.87    0.01    0.88    0.56    0.72    0.01
</code></pre>

<p>I want to return the <em>column name</em> which contains the minimum value of each row (of only B0, B1, B2):</p>

<pre><code>    A0      A1      A2      B0      B1      B2      C0      C1      col_of_min
0   0.84    0.47    0.55    0.46    0.76    0.42    0.24    0.75    B2
1   0.43    0.47    0.93    0.39    0.58    0.83    0.35    0.39    B0
2   0.12    0.17    0.35    0.00    0.19    0.22    0.93    0.73    B0
3   0.95    0.56    0.84    0.74    0.52    0.51    0.28    0.03    B2
4   0.73    0.19    0.88    0.51    0.73    0.69    0.74    0.61    B0
5   0.18    0.46    0.62    0.84    0.68    0.17    0.02    0.53    B2
6   0.38    0.55    0.80    0.87    0.01    0.88    0.56    0.72    B1
</code></pre>

<p>What's the best way to do this?</p>

<p>you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.filter.html">filter()</a> in conjunction with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.idxmin.html">idxmin()</a> method:</p>

<pre><code>In [40]: x
Out[40]:
     A0    A1    A2    B0    B1    B2    C0    C1
0  0.84  0.47  0.55  0.46  0.76  0.42  0.24  0.75
1  0.43  0.47  0.93  0.39  0.58  0.83  0.35  0.39
2  0.12  0.17  0.35  0.00  0.19  0.22  0.93  0.73
3  0.95  0.56  0.84  0.74  0.52  0.51  0.28  0.03
4  0.73  0.19  0.88  0.51  0.73  0.69  0.74  0.61
5  0.18  0.46  0.62  0.84  0.68  0.17  0.02  0.53
6  0.38  0.55  0.80  0.87  0.01  0.88  0.56  0.72

In [41]: x['col_of_min'] = x.filter(like='B').idxmin(axis=1)

In [42]: x
Out[42]:
     A0    A1    A2    B0    B1    B2    C0    C1 col_of_min
0  0.84  0.47  0.55  0.46  0.76  0.42  0.24  0.75         B2
1  0.43  0.47  0.93  0.39  0.58  0.83  0.35  0.39         B0
2  0.12  0.17  0.35  0.00  0.19  0.22  0.93  0.73         B0
3  0.95  0.56  0.84  0.74  0.52  0.51  0.28  0.03         B2
4  0.73  0.19  0.88  0.51  0.73  0.69  0.74  0.61         B0
5  0.18  0.46  0.62  0.84  0.68  0.17  0.02  0.53         B2
6  0.38  0.55  0.80  0.87  0.01  0.88  0.56  0.72         B1
</code></pre>

python pandas dataframe calculated-columns