Skip to content
Advertisement

Visualize each row of pandas data frame as a tree

I have a data frame having 4 columns A, B, C, D. I need to visualize/ print each row of my data frame as a tree structure.

Example:

df[‘A’] = franchisee

df[‘B’] = sign off

df[‘C’] = status

df[‘D’] = registration

Then, I need to visualize this row as a tree where franchisee is the parent node of sign off, sign off is the parent node of status and child node of franchisee, status is the parent node of registration and child node of sign off, registration is the child node of status. How can I do that? I have tried using the anytree library:

from anytree import Node, RenderTree, NodeMixin
import pandas as pd

df = pd.read_csv('file_location')
for i, j, k, l in zip(df['A'], df['B'], df['C'], df['D']):
    mdept = Node(a)
    dept = Node(b, parent = a)
    qtype = Node(c, parent = b)
    sqtype = Node(d, parent = c)

but it shows the following error:

Exception has occured:TreeError

Parent node ‘franchisee’ is not of type ‘NodeMixin’.

Advertisement

Answer

I didn’t know this library, so I can’t ensure there is not a better way, but iterating trough the rows and columns and using a root node:

from anytree import Node
root = Node('root')
for _, row in df.iterrows():
    n = Node(row['A'], parent=root)
    for c in ['B', 'C', 'D']:
        n = Node(row[c], parent=n) # n is always the previous node

# export as dot graph
from anytree.exporter import DotExporter
DotExporter(root).to_picture('graph.png')

output:

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement