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
There are two parts to your question.
1. Renaming the Node
Regarding renaming the node by using
Unique Nameas the alias forParentname, the above answer on aliasDict is good but we can modify the DataFrame directly instead, leaving your code unchanged.I have modified your DataFrame because it does not seem to run properly, and your code example does not clearly show that
Unique Nameis an alias forParentin some cases.2. Exporting to DataFrame
In the second part,
anyTreedoes not provide integration with pandas DataFrame. An alternative bigtree Python package does this out-of-the-box for you.The whole code example can be implemented as such,
Source: I'm the creator of bigtree ;)