title: Python Pandas updating same named columns and also done other calculation while in loop
body: <p>In dataframe, I want to iterate over same named columns and while iterating, when their sum exceeds "val_n" value. I want 4 things:
  1) exceed_when (at what iteration it exceed from "val_n" value)
  2) sum_col (sum of same named columns)
  3) At the point of exceed when, I want to replace corresponding col value as (col - (sum_col - val_n)
  4) And after exceed_when point, I want to replace rest of cols value to 0.</p>
  
  <p>Dataframe look like:</p>
  
  <pre><code>id  col1    col2    col3    col4    col5    col6    col7    col8    col9    col10   col11   col12   col13   col14   val_n
  1   350 350 350 350 350 350 350 350 350 350 0   0   0   0   3105.61
  2   50  50  55  105 50  0   50  100 50  50  50  50  1025    1066.86 3185.6
  3   0   0   0   0   0   3495.1  0   0   0   0   0   0   0   3495.1  3477.76
  </code></pre>
  
  <p>Required Dataframe:</p>
  
  <pre><code>id  col1    col2    col3    col4    col5    col6    col7    col8    col9    col10   col11   col12   col13   col14   val_n   exceed_when sum_col
  1   350 350 350 350 350 350 350 350 305.61  0   0   0   0   0   3105.61 9   3500
  2   50  50  55  105 50  0   50  100 50  50  50  50  1025    1066.86 3185.6      2751.86
  3   0   0   0   0   0   3477.76 0   0   0   0   0   0   0   0   3477.76 6   6990.2
  </code></pre>
  
  <p>This is what I have tried:</p>
  
  <pre><code>def trans(row):
      row['sum_col'] = 0
      row['exceed_ind'] = 0
  
      for i in range(1, 15):
          row['sum_col'] += row['col' + str(i)]
  
          if ((row['exceed_ind'] == 0) &amp;
              (row['sum_col'] &gt;= row['val_n'])):
  
              row['exceed_ind'] = 1
              row['exceed_when'] = i
  
          else:
              continue
  
          if row['exceed_when'] == i:
              row['col' + str(i)] = (
                  row['col' + str(i)] - (
                      row['sum_col'] - row['val_n']))
  
          elif row['exceed_when'] &lt; i:
              row['col' + str(i)] = 0
  
          else:
              row['col' + str(i)] = row['col' + str(i)]
      return row
  
  df1 = df.apply(trans, axis=1)
  </code></pre>
  
  <p>I am getting right results for sum_col, exceed when but conditions elif row['exceed_when'] &lt; i , doesn't seems to be working and its not updated the expected 4th point i.e. replace rest of cols value to 0. I am NOT sure what I miss.</p>
  
  <p>DDL to generate DataFrame:</p>
  
  <pre><code>import pandas as pd
  
  df = pd.DataFrame({'id': [1, 2, 3],
                     'col1': [350, 50, 0],
                     'col2': [350, 50, 0],
                     'col3': [350, 55, 0],
                     'col4': [350, 105, 0],
                     'col5' : [350, 50, 0],
                     'col6': [350, 0, 3495.1],
                     'col7': [350, 50, 0],
                     'col8': [350, 100, 0],
                     'col9': [350, 50, 0],
                     'col10': [350, 50, 0],
                     'col11': [0, 50, 0],
                     'col12': [0, 50, 0],
                     'col13': [0, 1025, 0],
                     'col14': [0, 1066.86, 3495.1],
                     'val_n': [3105.61, 3185.6, 3477.76]
                     })
  </code></pre>
  
  <p>Thanks!</p>