The given input is like:
EMPLOYEE_ID NAME MANAGER_ID
101 A 10
102 B 11
10 C 1
11 D 1
1 E null
Employee Cycle LEVEL Path
101 A 101/10/1
102 B 102/11/1
10 C 10/1
11 D 11/1
1 E 1
It will be great if one can solve it using python "pandas" library. I am not sure if it can be achieved using pandas or not. Other solutions are also welcomed.
dictionary
withEMPLOYEE_ID
andMANAGER_ID
:function
to create hierarchy stringapply
You can create a dictionary mapping children to parents.
Then use
pd.Series.apply
to construct your path string via awhile
loop.Note I assume
null
actually meansNaN
, which makes more sense for a numeric column.I find the method/approach used by user3483203 pretty neat and to the point; the code is simple to follow. The only thing that I'd add is instead of the function returning a '/' delimited string, I'd output a native python structure like a list. Something like this:
The output will look like this:
You can always wrap/change the list, if necessary, to comply with your desired output. However, now you can now just as easily quantify the distance/level of managerial hierarchy (how many layers apart is the CEO from the lowest staff) by using the len() function. By the way, I used a recursive approach. To scale it out, I'd stick to an iterative solution