title: looking for Python for-loop or while-loop which is able to iterates through columns in a data frame
body: <pre><code>m=[0, 0, 1, 0, 0, 0, 3, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0]
  names=[lev, yan, coke , coke, yan, lev, lev, yan, lev, yan, lev, yan, coke, coke, coke, lev, yan, lev, lev, yan, coke]
  n=[1, 1, 3, 4, 1, 1, 2, 1, 3, 1, 4, 2, 3, 4, 1, 1, 1, 1, 1, 2, 3]
  </code></pre>
  <p>I am looking for an iterative loop that adds up values of column n if the value in column m is non-zero.</p>
  <p>For example, at the 3rd place of column m, the value is 1 (non-zero) so it should add in the column n from index 0 to 2 i.e. 1+1+3=5. Similarly, at m[6]=3 (non-zero) this implies 4+1+1+2=8 and so on for distinct names in the names column so that it takes on name time and do the above iteration.</p>
  <p>Here is my code:</p>
  <pre><code>starting_idx = 0 
  sum_lst = [] 
  for idx, val in enumerate(m): 
      if val != 0: 
          sum_lst.append(sum(n[starting_idx: (idx+1)])) 
          starting_idx = idx+1 
      elif val==0: 
          sum_lst.append(0) 
  print(sum_lst)
  </code></pre>