title: how to perform arithmetic in pandas, when the columns involved change depending on the row. Summing Columns of Varying Length
body: <pre><code># Create a test data set based on the below dictionary
  import pandas as pd
  import math
  import random
  num_of_rows = 10000
  data = {
      'x1' : [random.uniform(0.0, 1.0) for x in range(num_of_rows)],
      'x2' : [random.uniform(0.0, 1.0) for x in range(num_of_rows)],
      'x3' : [random.uniform(0.0, 1.0) for x in range(num_of_rows)],
      'term': [random.randint(1, 3) for x in range(num_of_rows)]
  }
  df = pd.DataFrame(data)
  df.head()
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">x1</th>
  <th style="text-align: center;">x2</th>
  <th style="text-align: center;">x3</th>
  <th style="text-align: right;">term</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">0.103324</td>
  <td style="text-align: center;">0.304647</td>
  <td style="text-align: center;">0.979813</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.420082</td>
  <td style="text-align: center;">0.416356</td>
  <td style="text-align: center;">0.848054</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.722017</td>
  <td style="text-align: center;">0.888290</td>
  <td style="text-align: center;">0.728066</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.796869</td>
  <td style="text-align: center;">0.535150</td>
  <td style="text-align: center;">0.833837</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.764554</td>
  <td style="text-align: center;">0.244415</td>
  <td style="text-align: center;">0.479697</td>
  <td style="text-align: right;">1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <pre><code>%%timeit
  # To goal is to sum as month columns as indicated by the term column
  def sum_rows(list = [], cnt = 0):
      return sum(list[:int(cnt)])
  df['sum'] = df.apply(lambda x: sum_rows([x['x1'], x['x2'], x['x3']], x['term']), axis=1)
  df
  </code></pre>
  <p>output - 217 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)</p>
  <pre><code>%%timeit
  #  This appraoch advoids the apply function, but is very confusing to read
  df['sum'] = df['x1'] * (df['term'] &gt;= 1) + df['x2'] * (df['term'] &gt;= 2) + df['x3'] * (df['term'] &gt;= 3)
  df
  </code></pre>
  <p>output - 1.89 ms ± 6.03 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)</p>
  <p>The second approach is shown to be over 100 times quicker than using the apply function
  Are there alternative approaches to performing the above calculation what would be considered superior?
  What if I wanted to multiply the columns (instead of adding), the second approach will not longer give work</p>
  <p>Output of sum is :</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">x1</th>
  <th style="text-align: center;">x2</th>
  <th style="text-align: center;">x3</th>
  <th style="text-align: center;">term</th>
  <th style="text-align: right;">Sum</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">0.103324</td>
  <td style="text-align: center;">0.304647</td>
  <td style="text-align: center;">0.979813</td>
  <td style="text-align: center;">3</td>
  <td style="text-align: right;">1.387784</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.420082</td>
  <td style="text-align: center;">0.416356</td>
  <td style="text-align: center;">0.848054</td>
  <td style="text-align: center;">2</td>
  <td style="text-align: right;">0.836438</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.722017</td>
  <td style="text-align: center;">0.888290</td>
  <td style="text-align: center;">0.728066</td>
  <td style="text-align: center;">3</td>
  <td style="text-align: right;">2.338373</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.796869</td>
  <td style="text-align: center;">0.535150</td>
  <td style="text-align: center;">0.833837</td>
  <td style="text-align: center;">2</td>
  <td style="text-align: right;">1.332019</td>
  </tr>
  <tr>
  <td style="text-align: left;">0.764554</td>
  <td style="text-align: center;">0.244415</td>
  <td style="text-align: center;">0.479697</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">0.764554</td>
  </tr>
  </tbody>
  </table>
  </div>