title: Python: dynamic column sum for each row
body: <p>I have a dataframe with 2 identifiers (ID1, ID2) and 3 numeric columns (X1,X2,X3) and a column titled 'input' (total 6 columns) and n rows. For each row, I want to get the index of the nth column such that n is the last time that (x1+x2+xn... >=0) is still true.</p>
  
  <p>How can I do this in Python? </p>
  
  <p>In R I did this by using:</p>
  
  <pre><code>tmp = data
  
  
  for (i in 4:5)
  
  {
  
  data[,i]&lt;-  tmp$input - rowSums(tmp[,3:i])
  
  }
  
  output&lt;-  apply((data[,3:5]), 1, function(x) max(which(x&gt;0)))
  
  data$output &lt;-  output
  </code></pre>
  
  <p>I am trying to translate this into Python. What might be the best way to do this? There can be N such rows, and M such columns.</p>
  
  <p>Sample Data:</p>
  
  <pre><code>ID1 ID2 X1  X2  X3  INPUT   OUTPUT  (explanation)       
  a   b   1   2   3   3   2   (X1 = 1, x1+x2 = 3, x1+x3+x3 = 6 ... and after 2 sums, input&lt; sums)     
  a1  a2  5   2   1   4   0   (X1 = 5, x1+x2 = 7, x1+x3+x3 = 8 ... and even for 1 sum, input&lt; sums)   
  a2  b2  0   4   5   100 3   (X1=0, X1+X2=4, X1+X2+X3=9, ... even after 3 sums, input&gt;sums)
  </code></pre>