如果不存在,首先创建节点,将它们的引用存储在字典中nodes以供进一步使用。必要时为孩子更换父母。Parent我们可以通过查看哪些值不在值中来推导树木森林的根Child,因为父节点不是任何节点的子节点,它不会出现在Child列中。
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=[["A","A1"],["A","A2"],["A2","A21"],["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))
结果:
A
├── A1
└── A2
└── A21
B
└── B1
更新打印特定的根:
root = 'A' # change according to usecase
for pre, _, node in RenderTree(nodes[root]):
print("%s%s" % (pre, node.name))