Assume I have 2 columns:
OP_CODE Additional Work
a b
a c
a
b d
b e
c
d f
d g
d
e
f
g
What I have to do is, I have to take a value from column OP_Code and get all corresponding values with Additional Work. Now Additional Work column values are my new OP_Codes. Then search for its corresponding Additional Works again. This process continues until I come across a blank cell for that particular column. The output of above table looks like below: https://i.stack.imgur.com/SycQM.png The thing is we dont know how many branches can a tree go on. Below is my code sample:
from anytree import Node, RenderTree
udo = Node("019201")
x = df_asra[df_asra.op_code == udo.name]
First_Level = {}
for i in range(0,len(x)):
First_Level[i] = Node(x['Additional Work'].iloc[i], parent=udo)
for j in range(0,len(First_Level)):
y = df_asra[df_asra['op_code'] == (First_Level[j]).name]
Second_Level = {}
while(y['Additional Work'].empty == False):
for k in range(0,len(y)):
Second_Level[k] = Node(y['Additional Work'].iloc[k], parent = First_Level[j])
for l in range(0,len(Second_Level)):
z = df_asra[df_asra['op_code'] == (Second_Level[l]).name]
y = z
First_Level = Second_Level
for pre, fill, node in RenderTree(udo):
print("%s%s" % (pre, node.name))
Now I am getting output like this:
019201
├── 037061
│ ├── 015765
│ ├── 015795
│ │ ├── 013032
│ │ ├── 014270
│ │ ├── 015768
│ │ │ └── nan
│ │ └── nan
│ ├── nan
│ ├── nan
│ ├── nan
│ └── nan
├── nan
└── nan
Where Nan is blank column Now my problem is all branches are not in output. For example 014270 has further branches. It only runs for some parts. Plz Help