title: Python find pairs when from subset (pair numbers are changing)
body: <p>I have the following dataset:</p>
  <pre><code>d1 = {'Indiv1':['Subject1','Subject2','Subject1','Subject1','Subject2','Subject1','Subject1','Subject2'],
  'Indiv2': ['Subject4','Subject3','Subject2','Subject4','Subject4','Subject2','Subject3','Subject3'],
  'Event':['1','1','2','2','2','3','3','3'],
  'Category':['1','2','1','1','1','2','2','2'],
  'Variable1':['1','2','3','4','5','6','7','8'],
  'Variable2':['12','11','10','9','8','7','6','5'],
  'Variable3': ['-4','-3','-2','-1','0','1','2','3']}
  d1 = pd.DataFrame(d1)  
  d1=d1[['Indiv1','Indiv2','Event','Category','Variable1','Variable2','Variable3']]
  d1
  </code></pre>
  <p>Which gives the following (In my dataset this particular file has <strong>over 2 million rows</strong>):</p>
  <p>d1=</p>
  <p><a href="https://i.stack.imgur.com/MvSB7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MvSB7.jpg" alt="enter image description here" /></a></p>
  <p>I have a second smaller dataset <strong>(about 1500 rows)</strong> in the following format:</p>
  <pre><code>d2 = {'Indiv1': ['Subject1','Subject3','Subject1','Subject4','Subject2','Subject1','Subject1','Subject2'],
  'Indiv2': ['Subject4','Subject2','Subject6','Subject1','Subject1','Subject8','Subject9','Subject113'],
  'Event':['1','1','2','2','2','3','3','3'],
  'Category':['1','2','1','1','1','2','2','2']}
  d2 = pd.DataFrame(d2)
  d2=d2[['Indiv1','Indiv2','Event','Category']]
  d2
  </code></pre>
  <p>which looks like the following:</p>
  <p>d2=</p>
  <p><a href="https://i.stack.imgur.com/qfrPq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qfrPq.jpg" alt="enter image description here" /></a></p>
  <p>what I need to do is to look up the pairs of subjects in each event in each category from the second file (d2). If the pair exixts in both d1 and d2 for the given event id and category, then assign 1 to the row in df1. Otherwise assign 0.</p>
  <p><a href="https://i.stack.imgur.com/dTwjg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dTwjg.jpg" alt="enter image description here" /></a></p>
  <p>Note that in row 2 of df2, the order of the two individuals are flipped. Instead of Subject 2 and Subject 3 as in d1, in d2 we have Subject 3 and Subject 2. However, in my case I want to treat both as the same. In this case, I want to assign a value of 1 to these cases.</p>
  <p><a href="https://i.stack.imgur.com/h5ChD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h5ChD.jpg" alt="enter image description here" /></a></p>
  <p>Finally there are pairs in d1 which are not in d2 (for each event, for each category). For example, for event 3, there is no pair with Subject1 and Subject 2 in df2 (although this exists in df1). In such a case, assign a value =0 under the column present. The final output will look like this:</p>
  <p><a href="https://i.stack.imgur.com/KNHxS.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KNHxS.jpg" alt="enter image description here" /></a></p>
  <p>I am especially very perplexed with how to do it. Especially in the case, when groupings may change (Subject1 and Subject 2) vs (Subject2 and Subject 1).</p>
  <p>Any help will be appreciated. Ty in advance.</p>
  <p>Please let me know if I amam unclear</p>