I have a dataframe as
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)
I want to convert this dataframe to a tree, wherein each row is a path from root node to a leaf node.
I have tried using the approach shown in Read data from a pandas dataframe and create a dataframe using anytree in python
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["Group1"],groupdf["Group2"]):
add_nodes(nodes, parent, child)
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.
Also
roots = list(groupdf[~groupdf["Group1"].isin(groupdf["Group2"])]["Group1"].unique())
for root in roots:
for pre, _, node in RenderTree(nodes[root]):
print("%s%s" % (pre, node.name))
How to add the subsequent columns "Group3" and "Count to this tree structure?