I am trying to go through a hierarchy dataframe and record every possible routes into another dataframe. These routes can have variable depth.
Original dataframe (df). The highest column means that the value in the parent column is not a child of any:
| parent | child | highest |
|---|---|---|
| a | b | 1 |
| b | c | 0 |
| b | d | 0 |
| d | e | 0 |
End goal dataframe:
| level 3 | level 2 | level 1 | level 0 |
|---|---|---|---|
| a | b | c | |
| a | b | d | e |
This what I currently have
def search(parent):
for i in range(df.shape[0]):
if(df.iloc[i,0] == parent):
search(df.iloc[i,1])
for i in range(df.shape[0]):
if(df.iloc[i,2] == 1):
search(df.iloc[i,0])
I am able to go through the hierarchy but I do not know how to save it in the format I want.