title: Pandas error when using if-else to create new column: The truth value of a Series is ambiguous
body: <p>I'm using Pandas and am trying to create a new column using a Python if-else statement (aka ternary condition operator) in order to avoid division by zero.</p>
  
  <p>For example below, I want to create a new column C by dividing A/B. I want to use the if-else statement to avoid dividing by 0.</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  df = pd.DataFrame(np.random.randint(0, 5, size=(100, 2)), columns=list('AB'))
  df.head()
  #    A  B
  # 0  1  3
  # 1  1  2
  # 2  0  0
  # 3  2  1
  # 4  4  2
  
  df['C'] = (df.A / df.B) if df.B &gt; 0.0 else 0.0
  </code></pre>
  
  <p>However, I am getting an error from the last line:</p>
  
  <pre><code>ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
  </code></pre>
  
  <p>I searched on StackOverflow and found other posts about this error, but none of them involved this type of if-else statement. Some posts include:</p>
  
  <p><a href="https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o">Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()</a></p>
  
  <p><a href="https://stackoverflow.com/questions/45811610/the-truth-value-of-a-series-is-ambiguous-in-dataframe">The truth value of a Series is ambiguous in dataframe</a></p>
  
  <p><a href="https://stackoverflow.com/questions/45493948/error-the-truth-value-of-a-series-is-ambiguous-python-pandas">Error: The truth value of a Series is ambiguous - Python pandas</a></p>
  
  <p>Any help would be appreciated.</p>