In this post, we will see how to resolve Detect immediate parent rows of each row and add their values to a new column
Question:I have a dataframe that contains multiple hierarchy columns and a value attach to them, something like this:
|location hierarhcy||industry hierarchy||value|
Now I want to find the immediate parents of each row and add their value a a new column. My example just shows two parents but it could be more. So, the expected output would be as follows:
|location hierarhcy||industry hierarchy||value||value-parent-1||value-parent-2|
My Dataframe could be extremely large, so the speed and memory are important factors. So, row-wise process won’t really work.
The immediate parents for a row will be all rows that are one level higher than one of the hierarchies. For example for row 5, the potential immediate parents will be:
|–||location hierarhcy||industry hierarchy|
This is the code to create the dataframe if you want to play with it.
Best Answer:Here you are:
If you have better answer, please add a comment about this, thank you!