title: Group connected graphs in pandas DF
body: <p>I have a pandas DF where each column represent a node and two columns an edge, as following: </p>
  
  <pre><code> import pandas as pd
  df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'],
                 'node2': ['4', '13', '25', '38', '208', '300']})
  </code></pre>
  
  <p>All Nodes are Undirected, i.e. you can get from one to the other <a href="https://mathinsight.org/definition/undirected_graph" rel="noreferrer">undirected_graph</a> </p>
  
  <p>I would like to group them into all connected groupes <a href="https://en.wikipedia.org/wiki/Connectivity_(graph_theory)" rel="noreferrer">(Connectivity)</a>, as following:</p>
  
  <pre><code>df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'],
             'node2': ['4', '13', '25', '38', '208', '300']
              ,'desired_group': ['1', '1', '2', '2',  '3', '3']})
  </code></pre>
  
  <p>For example, the reason why the first two rows were grouped, is because its possible to get from node 2 to node 13 (through 4).</p>
  
  <p>The closest question that i managed to find is this one:
  <a href="https://stackoverflow.com/questions/35089255/pandas-reshape-dataframe-to-edge-list-according-to-column-values">pandas - reshape dataframe to edge list according to column values</a> but to my understanding, its a different question.</p>
  
  <p>Any help on this would be great, thanks in advance.</p>