I am working on this project and I need to create columns with hierarchal relationship to each other in pandas based on the original two columns that indicates hierarchal relationships of each value.
| supervisory_org | Superior_org | |
|---|---|---|
| 0 | org_2 | org_1 |
| 1 | org_7 | org_3 |
| 2 | org_4 | org_2 |
| 3 | org_6 | org_3 |
| 4 | org_9 | org_5 |
| 5 | org_3 | org_1 |
| 6 | org_5 | org_3 |
| 7 | org_8 | org_5 |
Above are the two original columns that indicates the relationship between two organizations(values). I want to make those hierarchal relationship between orgs more visible by spreading across multiple columns as below. (Below is the desired output)
| Level_1 | Level_2 | Level_3 | Level_4 | |
|---|---|---|---|---|
| 0 | org_1 | org_2 | org_4 | NaN |
| 1 | org_1 | org_3 | org_6 | NaN |
| 2 | org_1 | org_3 | org_7 | NaN |
| 3 | org_1 | org_3 | org_5 | org_8 |
| 4 | org_1 | org_3 | org_5 | org_9 |
I'm trying to make this work in pandas but still haven't come up with a way to do it. Anyone have any suggestions on how to approach this problems?
This is the code for creating the first table
df = pd.DataFrame({"supervisory_org" : ["org_2","org_7" ,"org_4", "org_6","org_9","org_3","org_5", "org_8"],
"Superior_org" : ["org_1","org_3","org_2","org_3","org_5","org_1","org_3","org_5"]})