Construct anytree from pandas DataFrame with multiple parents

522 views Asked by At

I've got a huge DataFrame. This just an example. But you can see that "b" element has "a", "d", "k" parents.

data = pd.DataFrame(columns=["Parent","Child"], data=[["a","b"],["a","d"],["d","v"],["d","b"],["c","f"],["b","n"],["s","c"],["k","b"],["n","k"]])

Read data from a pandas DataFrame and create a tree using anytree in python

In this solution tree is building, but parents overwriting.

How to build multiple parents tree? P.S Maybe my array is bad.

1

There are 1 answers

0
txp On

Because anytree is a pure tree library, it supports only a single parent per node.

If you want to assign multiple parents to a node, you're looking at a directed graph. To work with such graphs in Python, try networkx