title: How to attribute to each elements of a sublist sharing comment elements the unique ID of the related sublist?
body: <p>Out of an list of ~500k lines made of pairs of items, I'm trying to build up a file that aims to allocate for each item an ID related to the group they belong to. <em>Further explanations follow.</em></p>
  
  <p>And I would need some help to get the result in a smart and efficient way (that is pythonic)</p>
  
  <p>==============</p>
  
  <h3>what I want to do</h3>
  
  <p>Transform the <strong>input</strong> file df0 into the <strong>desired output</strong> df2</p>
  
  <p>For instance, the starting file would look like this (but with 500k entries), where a relation from item1 to item2 is determined by the structure of the dataframe.</p>
  
  <p><strong>df0 : input</strong></p>
  
  <pre><code>df0 = pd.DataFrame({
  "item 1": ['Q', 'R', 'B', 'A'],
  "item 2": ['R', 'P', 'A', 'C']
  })
  </code></pre>
  
  <p>It reads as following: item Q is related to item R, and item R is related to item P, hence item Q is related to item P (same with with A, B and C). In that case, the transitivity of relationships leads to build two groups of items.</p>
  
  <ul>
  <li>Each item belongs to only 1 group.</li>
  <li>It is expected in the real case file that groups can hold up to 11 items.</li>
  <li>the alphabetic order plays no role</li>
  </ul>
  
  <hr>
  
  <p>Thanks to other contributions on stackoverflow, I managed to group all transitive items into single sets and allocate them a single group number (or ID). Meaning I get a dataframe that looks like that:</p>
  
  <pre><code>df1 = pd.DataFrame({
  "items": [{'Q', 'R', 'P'}, {'B', 'A', 'C'} ],
  "group": [1, 2]
  })
  </code></pre>
  
  <hr>
  
  <p>The result above is now to be transformed to support further data post-treatments and the desired outcome should look like this:</p>
  
  <p><strong>df2 : desired output</strong></p>
  
  <pre><code>df2 = pd.DataFrame({
  "items": ['Q', 'R', 'P', 'B', 'A', 'C' ],
  "group": [1, 1, 1, 2, 2, 2 ]
  })
  </code></pre>
  
  <p>==============</p>
  
  <h3>What I managed so far</h3>
  
  <p><strong>step 1 :</strong> convert df1.item into a series of single items</p>
  
  <pre><code>d = df1.item
  e = list(sorted(set(chain.from_iterable(d))))
  df2 = pd.DataFrame({'item':e})
  </code></pre>
  
  <p><strong>step 2 :</strong> 'vlookup' df2.items back to df1.group via df1.items</p>
  
  <pre><code>df2['group'] = ''  
  
  n = 0
  for row in df2.items :
  m = 0
  for row in df1.items :
      if df2['items'][n] in df1['items'][m]:
          df2['group'][n] = df1['group'][m]
      m = m + 1
  n = n + 1
  </code></pre>
  
  <p>==============</p>
  
  <p><strong>It does work for small tables, but is not workable on large dataframes.</strong></p>
  
  <p>I'm now looking for assistance regarding :</p>
  
  <ul>
  <li>either a better code for step 2 to enhance df2 <em>(preferred)</em></li>
  <li>or a better way to jump over step 2 and get df2 straight out of df1</li>
  </ul>
  
  <p><em>Thank you very in advance for your time and feedback !</em></p>