Conditionally calculated column for a Pandas DataFrame
<p>I have a calculated column in a Pandas DataFrame which needs to be assigned base upon a condition.  For example:</p>

<pre><code>if(data['column_a'] == 0):
    data['column_c'] = 0
else:
    data['column_c'] = data['column_b']
</code></pre>

<p>However, that returns an error:</p>

<pre>
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
</pre>

<p>I have a feeling this has something to do with the fact that is must be done in a matrix style.  Changing the code to a ternary statement doesn't work either:</p>

<pre><code>data['column_c'] = 0 if data['column_a'] == 0 else data['column_b']
</code></pre>

<p>Anyone know the proper way to achieve this?  Using apply with a lambda?  I could iterate via a loop, but I'd rather keep this the preferred Pandas way.</p>

<p>use where() and notnull() </p>

<pre><code>   data['column_c'] = data['column_b'].where(data['column_a'].notnull(), 0)
</code></pre>

python pandas dataframe