Use a list to conditionally fill a new column based on values in multiple columns
<p>I am trying to populate a new column within a pandas dataframe by using values from several columns. The original columns are either <code>0</code> or '1' with exactly a single <code>1</code> per series. The new column would correspond to df['A','B','C','D'] by populating <code>new_col = [1, 3, 7, 10]</code> as shown below. (A <code>1</code> at <code>A</code> means <code>new_col = 1</code>; if <code>B=1</code>,<code>new_col = 3</code>, etc.)</p>

<pre><code>df    
         A    B    C    D
1        1    0    0    0
2        0    0    1    0
3        0    0    0    1
4        0    1    0    0
</code></pre>

<p>The new <code>df</code> should look like this.</p>

<pre><code>df    
         A    B    C    D   new_col
1        1    0    0    0         1
2        0    0    1    0         7
3        0    0    0    1        10
4        0    1    0    0         3
</code></pre>

<p>I've tried to use <code>map</code>, <code>loc</code>, and <code>where</code> but can't seem to formulate an efficient way to get it done. Problem seems very close <a href="http://stackoverflow.com/questions/36551128/how-to-fill-a-column-conditionally-to-values-in-an-other-column-being-in-a-list">to this</a>. A couple other posts I've looked at <a href="http://stackoverflow.com/questions/10715519/conditionally-fill-column-values-based-on-another-columns-value-in-pandas">1</a> <a href="http://stackoverflow.com/questions/19226488/python-pandas-change-one-value-based-on-another-value">2</a> <a href="http://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas">3</a>. None of these show how to use multiple columns conditionally to fill a new column based on a list.</p>

<p>It's not the most elegant solution, but for me it beats the if/elif/elif loop:</p>

<pre><code>d = {'A': 1, 'B': 3, 'C': 7, 'D': 10}
def new_col(row):
    k = row[row == 1].index.tolist()[0]
    return d[k]

df['new_col'] = df.apply(new_col, axis=1)
</code></pre>

<p>Output:</p>

<pre><code>    A   B   C   D   new_col
1   1   0   0   0   1
2   0   0   1   0   7
3   0   0   0   1   10
4   0   1   0   0   3
</code></pre>

python list python-2.7 pandas