title: Python / Pandas: Calculation based on cell value
body: <p>I have data frame like this:</p>
  
  <pre><code>   A B C D E
  0  2 3 4 8 7
  1  4 7 5 9 4 
  2  3 4 5 7 2
  3  8 9 1 3 7
  </code></pre>
  
  <p>I need to do something like this:</p>
  
  <pre><code>if 'value in column A' == 2:
    'value for this row in new column' = 'value from column B' + 'value from column C'
  elif 'value in column A' == 4:
     'value for this row in new column' = 'value from column B' + 'value from column D'
  elif 'value in column A' == 8:
     'value for this row in new column' = 'value from column B' + 'value from column E'
  else:
     'value for this row in new column' = 0
  </code></pre>
  
  <p>I tried to do this in few ways, e.g.:</p>
  
  <pre><code>1.
  df['sum'][df['A'] == 2] = df['B'] + df['C']
  df['sum'][df['A'] == 4] = df['B'] + df['D']
  df['sum'][df['A'] == 8] = df['B'] + df['E']
  
  2.
  df.loc[df['A'] == 2, 'sum'] = df['B'] + df['C']
  df.loc[df['A'] == 4, 'sum'] = df['B'] + df['D']
  df.loc[df['A'] == 8, 'sum'] = df['B'] + df['E']
  </code></pre>
  
  <p>but I had empty cells in result.</p>