title: Updating pandas index and row in nested while loop
body: <p>I am facing the challenge iteratively updating <code>row</code> and <code>index</code> in a for loop that makes use of the <code>pd.iterrows()</code> generator.  In the example below, my objective is to get the distance between each consecutive letter and the <em>first</em> letter (A) starting from index 0:</p>
  <pre><code>import pandas as pd
  import string
   
  data = {'letter':['A', 'C', 'D', 'X', 'X', 'Z', 'A', 'E', 'Z', 'Y', 'D', 'B', 'A']}
   
  start_idx=df['letter'].iloc[0]
  
  for index, row in df.iloc[1:].iterrows():
      
      dist= abs(string.ascii_uppercase.index(df['letter'].iloc[0]) - \
                string.ascii_uppercase.index(df['letter'].iloc[index]))
                
      print(dist)
  2
  3
  23
  23
  25
  0
  4
  25
  24
  3
  1
  0
  </code></pre>
  <p>That is easy enough. However, if the distance exceeds 5, then I would like to start comparing the following letters to the last &quot;normal&quot; letter whose distance from the previous was &lt;= 5 using a while loop and append the indexes of letters that deviate.  For example:</p>
  <pre><code>import pandas as pd
  import string
   
  data = {'letter':['A', 'C', 'D', 'X', 'X', 'Z', 'A', 'E', 'Z', 'Y', 'D', 'B', 'A']}
  bad_letters = [] 
      
  
  start_idx=df['letter'].iloc[0]
  compare_letter = string.ascii_uppercase.index(df['letter'].iloc[0])
  
  for index, row in df.iloc[1:].iterrows():
                                                
      dist= abs(compare_letter-string.ascii_uppercase.index(row['letter']))  
      
      if dist &gt; 5:
                                                
          compare_letter =  string.ascii_uppercase.index(df['letter'][index-1]) #reset compare letter
          abnormal=True
  
          while abnormal:
              
              bad_letters.append(index)
              dist=abs(compare_letter-string.ascii_uppercase.index(df['letter'][index]))
              index+=1 #increment index
              
              if dist &lt;=5:
                  abnormal=False
                  compare_letter=string.ascii_uppercase.index(df['letter'][index])
                  #?update iterrows index with this index#
                  break
              
          else:
              continue
         
  </code></pre>
  <p>The output list <code>bad_letters</code> <strong>should</strong> be: [3,4,5,8,9] which correspond to:</p>
  <p>-the index of letters <strong>X,X,Z</strong> which deviated more than 5 from D at index 2<br />
  -the index of letters <strong>Z,Y</strong> which deviated more than 5 from letter E at index 7.</p>
  <p>The above attempt fails, and I am not sure how to structure this properly in a way that effectively uses <code>iterrows()</code> with a while loop.  How would one use a while loop inside of <code>iterrows()</code> or a different pandas dataframe generator to answer this basic question?  How can one iteratively &quot;update&quot; the <strong>index and row</strong> of the original for loop with the index that breaks the nested while loop? Any advice would be appreciated.</p>