title: Python Pandas inconsistent results with nested for each iteration
body: <p>This is my first post and I apologise as I am struggling to make sense.</p>
  
  <p>I am using Pandas with Python to do data analysis and have an issue that I dont understand (and so am struggling to explain).</p>
  
  <p>I am calling a function from within a 'for each loop' which attempts to insert values into a Pandas DataFrame based on a specified column start and end location. The function is this:</p>
  
  <pre><code>cfg_broll_temp.ix[cfg_broll_temp['aged_debt'] == colb,scolb:ecolb] = tmp_brollb
  </code></pre>
  
  <p>And the error is the following:</p>
  
  <pre><code>ValueError: Must have equal len keys and valuewhen setting with an iterable
  </code></pre>
  
  <p>I have tracked this down to an iteration I am doing within the function that is being called. I am running through a for each loop based on an item list, and for each item running a nested for each to step through a slice of a Pandas series (cfg_broll_sr[srowb:erowb]) in order to generate a result list (tmp_brollb) which I update to the DataFrame with in the failing statement.</p>
  
  <p>My issue is that despite the same starting conditions when I call this function it seems to generate a list of inconsistent length. e.g. with values of srowb = 1 and erowb = 18 it will generate a list (tmp_brollb) which has either len(tmp_brollb) = 17 or len(tmp_brollb) = 18</p>
  
  <p>I am banging my head against a wall with this as I am struggling to isolate any changes that cause this. </p>
  
  <p>The specific code block I am having issues with is:</p>
  
  <pre><code># Set starting conditions
  
  srowb = 1 # starting row
  if np.isnan(variable) :
      erowb = 18 
  else:
      erowb = int(variable)
  
  # iterate through list
  tmp_brollb = [] # Generate empty array
  z = 0 # reset starting value
  for itemr in cfg_broll_sr[srowb:erowb]:
      z = min(z + itemr,1) # prevent more that 100%
      tmp_brollb.append(z)
  
  # Increment
  
  srowb = srowb + 1
  erowb = min(erowb + 1,20)
  </code></pre>
  
  <p>Some more detail on the code here:</p>
  
  <pre><code># Set maximum number of rows and starting row
  rowsb = 18
  srowb = 1 # starting row
  
  # check if a event exists and set maximum row to cease value
  if np.isnan(event_month) :
      erowb = 18 
  else:
      erowb = int(event_month) 
  
  # Set starting conditions for range insertion 
  basecolb = 7 # starting column for roll data
  scolb = basecolb # Getting an expanding range of columns from 1 to 18
  ecolb = basecolb + erowb
  
  # item tuple for step through
  dcolsb = ('item1','item2','item3')
  
  # step through and update values
  for colb in dcolsb :
  
      # Generate accumulated arrays
      tmp_brollb = None # clear variable
      tmp_brollb = [] # Generate empty array
      z = 0 # reset starting value
      for itemr in cfg_broll_sr[srowb:erowb]:
          z = min(z + itemr,1) # prevent more that 100%
          tmp_brollb.append(z)
  
      # insert accumulated values into selected row and column range
      cfg_broll_temp.ix[cfg_broll_temp['item_column'] == colb,scolb:ecolb] = tmp_brollb
      # carry last value from tmp_brollb forward to end of column range
      cfg_broll_temp.ix[cfg_broll_temp['item_column'] == colb,ecolb:] = tmp_brollb[-1]
  
      # increment all positional variables
      srowb = srowb + 1
      erowb = min(erowb + 1,20)
      scolb = scolb + 1
      ecolb = min(ecolb + 1,25)
  </code></pre>
  
  <p>If I print the following I get:</p>
  
  <pre><code>    print "inconsistent length issue", len(tmp_brollb), erowb - srowb, srowb, erowb
  
  
  run strategy 17
  inconsistent length issue 18 17 1 18 # len(tmp_brollb) inconsistent despite same erowb - srowb value
  inconsistent length issue 17 17 2 19
  inconsistent length issue 16 17 3 20
  inconsistent length issue 15 16 4 20
  inconsistent length issue 14 15 5 20
  inconsistent length issue 13 14 6 20
  inconsistent length issue 12 13 7 20
  inconsistent length issue 11 12 8 20
  inconsistent length issue 10 11 9 20
  inconsistent length issue 9 10 10 20
  inconsistent length issue 8 9 11 20
  inconsistent length issue 7 8 12 20
  inconsistent length issue 6 7 13 20
  run strategy 17 again via parent iterator
  inconsistent length issue 17 17 1 18 # len(tmp_brollb) inconsistent despite same erowb - srowb value
  inconsistent length issue 17 17 2 19
  inconsistent length issue 16 17 3 20
  inconsistent length issue 15 16 4 20
  inconsistent length issue 14 15 5 20
  inconsistent length issue 13 14 6 20
  inconsistent length issue 12 13 7 20
  inconsistent length issue 11 12 8 20
  inconsistent length issue 10 11 9 20
  inconsistent length issue 9 10 10 20
  inconsistent length issue 8 9 11 20
  inconsistent length issue 7 8 12 20
  inconsistent length issue 6 7 13 20
  </code></pre>
  
  <p><strong>Any help would be much appreciated as I have a 4hr headache now and growing.</strong></p>