title: vectorizing nested for loops - pandas
body: <p>I have a case where multiple attributes from an 'outside' for-loop are compared to multiple attributes from an 'inside' for loop.</p>
  
  <p>Both loops are on pandas dataframes, and from a little reading, using <code>iterrows()</code> for this sort of a job is generally going to be slow.</p>
  
  <p>Below is an indication of how / why this nested for loop is being used. It is very slow.</p>
  
  <pre><code>for key1, values1 in dataframe_1.iterrows():        
  
      for key2, values2 in dataframe_2.iterrows():
  
          if values2['a'] &gt; values1['a'] and value2['b'] == values1['b']:
              # do something, such as append to a combined df
  </code></pre>
  
  <p>Is there a more suitable way to perform these sorts of nested comparisons on pandas dataframes? Is a different datatype (i.e. a dictionary) a better place to start?</p>