title: Python: Pandas merge on multiple sets of keys
body: <p>I'm trying to merge two data frames on multiple sets of keys. Meaning that I try to merge using first set, then I take what's left unmerged and use the second set and so on to see what's left unmerged after using all possible sets (in order). I was wondering if there's a quicker and more efficient way of doing it. Here's my current code:</p>
  <pre><code>################# SUBJECT + VISIT + TIME #################
  
  df1['key'] = df1['SUBJECT'] + df1['VISIT'] + df1['TIME']
  df2['key'] = df2['Subject'] + df2['VISIT'] + df2['TIME']
  part_one = pd.merge(df1, df2, how='inner', on='key')
  
  ################# SUBJECT + VISIT + TIMEPOINT #################
  
  df1 = df1[(df1['key'].isin(part_one['key']) == False)]
  df2 = df2[(df2['key'].isin(part_one['key']) == False)]
  df1['key'] = df1['SUBJECT'] + df1['VISIT'] + df1['TIMEPOINT']
  df2['key'] = df2['Subject'] + df2['VISIT'] + df2['TIMEPOINT']
  part_two = pd.merge(df1, df2, how='inner', on='key')
  
  ################# SUBJECT + DATE + TIME #################
  
  df1 = df1[(df1['key'].isin(part_two['key']) == False)]
  df2 = df2[(df2['key'].isin(part_two['key']) == False)]
  df1['key'] = df1['SUBJECT'] + df1['DATE'] + df1['TIME']
  df2['key'] = df2['Subject'] + df2['DATE'] + df2['TIME']
  part_three  = pd.merge(df1, df2, how='inner', on='key')
  
  ################# SUBJECT + DATE + TIMEPOINT #################
  
  df1 = df1[(df1['key'].isin(part_three['key']) == False)]
  df2 = df2[(df2['key'].isin(part_three['key']) == False)]
  df1['key'] = df1['SUBJECT'] + df1['DATE'] + df1['TIMEPOINT']
  df2['key'] = df2['Subject'] + df2['DATE'] + df2['TIMEPOINT']
  part_four = pd.merge(df1, df2, how='inner', on='key')
  
  ################# REMAINING ROWS #################
  
  left_df1 = df1[(df1['key'].isin(part_four['key']) == False)]
  left_df2 = df2[(df2['key'].isin(part_four['key']) == False)]
  </code></pre>