title: How do I speed up incredibly slow iteration through pandas dataframe?
body: <p>I have collected 500ms stock data for several weeks. No I am wanting to go through each days data an iterate through it to determine at any given last price value, how many times a specific lowerbound would be passed followed by a specific upperbound being passed for the rest of the day. The lowerbound has to be passed before the script starts searching for whether the upperbound will be reached. </p>
  
  <p>So essentially if the last price at row i is 10 and the lowerbound then is 9 and 11. The code will first try to find a moment when the remaining rows i+1...i+2... are reach the lowerbound, as soon as the lower bound is reached the code switches into looking for when the upperbound is reached. If the upperbound is reached then the success will add 1 and the code starts looking for the lowerbound again, doing this whole process again. </p>
  
  <p>This entire process occurs for every single row, so essentially for each row we will have a column for how many times a successful lower and upper bound reach occurred in the rows following that given row. </p>
  
  <p>The problem I am having is that I have about 14400 rows per day, and about 40 days so around 576000 rows of data. The iteration takes absolutely forever, and in order for me to do this across all of my data I will need my computer to run a few days. Surely I am not doing this in the most efficient way possible am I? Can anybody maybe point to a concept that I can rewrite this code in a much more effective way? Or am I just stuck waiting for ever for it to prepare my data? </p>
  
  <pre><code>    range_per = .00069 #the percentage determining lower and upper bound
      data['Success?']=np.nan
      data['Success? Count']=np.nan
  
      #For every row count how many times the trade in the range would be successful
      for i in range(0,len(data)):
          last_price = data.at[i,'lastPrice']
          lower_bound = last_price -  last_price*range_per
          upper_bound = last_price + last_price*range_per
          lower_bound_reached = False
          upper_bound_reached = False
          success=0
  
          for b in range(i+1,len(data)):
              last_price = data.at[b,'lastPrice']
              while lower_bound_reached == False:
                  if lower_bound - last_price &gt;=0:
                      upper_bound_reached = False
                      lower_bound_reached = True
  
                  else:
                      break
              while (upper_bound_reached == False and lower_bound_reached ==True):
                  if upper_bound - last_price &lt;=0:
                      success+=1
                      lower_bound_reached = False
                      upper_bound_reached = True
  
  
                  else:
                      break
          print('row %s: %s times' %(i, success))
          data['Success? Count'][i] = success
          if success&gt;0:
              data['Success?'][i] = True
          else: 
              data['Success?'][i] = False
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/UP23L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UP23L.png" alt="Here a a look at the last price row and the two output rows created by the code above"></a></p>