I have a dataset as follows
Unique Name | Parent | Child |
---|---|---|
US_SQ | A | A1 |
UC_LC | A | A2 |
UK_SJ | A2 | A21 |
UI_QQ | B | B1 |
Now I want to set the output as follows:
US_SQ
├── A1
└── UC_LC
└── UK_SJ
UI_QQ
└── B1
In other words, I want to use the Unique name
column value in the tree.
This is the code that I am using:
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]
data = pd.DataFrame(columns=["Parent","Child"], data=[["US_SQ","A","A1"],["UC_LC","A","A2"],["UK_SJ","A2","A21"],["UI_QQ","B","B1"]])
nodes = {} # store references to created nodes
# data.apply(lambda x: add_nodes(nodes, x["Parent"], x["Child"]), axis=1) # 1-liner
for parent, child in zip(data["Parent"],data["Child"]):
add_nodes(nodes, parent, child)
roots = list(data[~data["Parent"].isin(data["Child"])]["Parent"].unique())
for root in roots: # you can skip this for roots[0], if there is no forest and just 1 tree
for pre, _, node in RenderTree(nodes[root]):
print("%s%s" % (pre, node.name))
Also, is there a way to access the tree data efficiently/ is there any format to save the tree data so that we can easily find the parent/child node easily?
The above data and problem is used from here:
Read data from a pandas DataFrame and create a tree using anytree in python