title: Grouped Dataframe to a nested tree in python
body: <p>I have a dataframe as</p>
  <pre><code>grpdata = {'Group1':['A', 'A', 'A', 'B','B'],
          'Group2':['A2','B2','B2','A2','B2'],
          'Group3':['A3', 'A3', 'B3','A3', 'A3'],
          'Count':['10', '12', '14', '20']}
   
  # Convert the dictionary into DataFrame 
  groupdf = pd.DataFrame(grpdata)
  </code></pre>
  <p>I want to convert this dataframe to a tree, wherein each row is a path from root node to a leaf node.</p>
  <p>I have tried using the approach shown in
  <a href="https://stackoverflow.com/questions/64084786/read-data-from-a-pandas-dataframe-and-create-a-tree-using-anytree-in-python">Read data from a pandas dataframe and create a dataframe using anytree in python</a></p>
  <pre><code>def add_nodes(nodes, parent, child):
      if parent not in nodes:
          nodes[parent] = Node(parent)  
      if child not in nodes:
          nodes[child] = Node(child)
      nodes[child].parent = nodes[parent]
  
  nodes = {}  
  for parent, child in zip(groupdf[&quot;Group1&quot;],groupdf[&quot;Group2&quot;]):
  
      add_nodes(nodes, parent, child)
  </code></pre>
  <p>However I am not able to figure out how to add the Group3 as a child to Group2 as parent node in the same node structure defined above.</p>
  <p>Also</p>
  <pre><code>roots = list(groupdf[~groupdf[&quot;Group1&quot;].isin(groupdf[&quot;Group2&quot;])][&quot;Group1&quot;].unique())
  
  
  
  for root in roots:         
      for pre, _, node in RenderTree(nodes[root]):
      print(&quot;%s%s&quot; % (pre, node.name))
  </code></pre>
  <p>How to add the subsequent columns &quot;Group3&quot; and &quot;Count to this tree structure?</p>