title: Pandas check if row exist in another dataframe and append index
body: <p>I'm having one problem to iterate over my dataframe. The way I'm doing is taking a long time and I don't have that many rows (I have like 300k rows)</p>
  <p>What am I trying to do?</p>
  <ol>
  <li><p>Check if one DF (A) contains the value of two columns of the other DF (B). You can think of this as a multiple-key field</p>
  </li>
  <li><p>If True, get the index of <code>DF.B</code> and assign to one column of <code>DF.A</code></p>
  </li>
  <li><p>If False, two steps:</p>
  </li>
  </ol>
  <p>a. append to <code>DF.B</code> the two columns not found</p>
  <p>b. assign the new ID to <code>DF.A</code> (I couldn't do this one)</p>
  <p>This is my code, where:</p>
  <ol>
  <li><p>df is <code>DF.A</code> and df_id is <code>DF.B</code>:</p>
  </li>
  <li><p>SampleID and ParentID are the two columns I am interested to check if they exist in both dataframes</p>
  </li>
  <li><p>Real_ID is the column to which I want to assign the id of <code>DF.B</code> (df_id)</p>
  </li>
  </ol>
  <pre class="lang-py prettyprint-override"><code>for index, row in df.iterrows():
      #check if columns exist in the other dataframe
      real_id = df_id[(df_id['SampleID'] == row['SampleID']) &amp; (df_id['ParentID'] == row['ParentID'])]
      
      if real_id.empty:
          #row does not exist, append to df_id
          df_id = df_id.append(row[['SampleID','ParentID']])
      else:
          #row exists, assign id of df_id to df
          row['Real_ID'] = real_id.index
  </code></pre>
  <p>EXAMPLE:</p>
  <p>DF.A (df)</p>
  <pre><code>   Real_ID   SampleID   ParentID  Something AnotherThing
  0             20          21          a          b      
  1             10          11          a          b      
  2             40          51          a          b       
  </code></pre>
  <p>DF.B (df_id)</p>
  <pre><code>   SampleID   ParentID  
  0    10          11         
  1    20          21     
  </code></pre>
  <p><strong>Result</strong>:</p>
  <pre><code>   Real_ID   SampleID   ParentID  Something AnotherThing
  0      1      10          11          a          b      
  1      0      20          21          a          b      
  2      2      40          51          a          b      
  
  
     SampleID   ParentID  
  0    20          21         
  1    10          11    
  2    40          51
  </code></pre>
  <p>Again, this solution is very slow. I'm sure there is a better way to do this and that's why I'm asking here. Unfortunately this was what I got after some hours...</p>
  <p>Thanks</p>