I have a dataframe that is very similar to this question with the caveat that:
An employees level is not known
The order of employees is random
Because of (1) and (2), there may instances where the employee is parsed before their supervisor
I was using this answer as my basis but because of my caveats, there are a lot of instances where an employee is just under the top level because when supervisor = cache.get(supervisor_key(row), {})
is run, the supervisor has not been added yet and the .get()
defaults to {}
.
How do I dynamically shift a nested dict to insert a supervisor?
Edit: Sample data is same as linked question but the order has been changed and level is unknown
Employee_FN Employee_LN Supervisor_FN Supervisor_LN4 Pam Beasley Jim Halpert0 Michael Scott None None7 Meredith Palmer Ryan Howard1 Jim Halpert Michael Scott2 Dwight Schrute Michael Scott3 Stanley Hudson Jim Halpert5 Ryan Howard Pam Beasley6 Kelly Kapoor Ryan Howard
Output is:
[{'Employee_FN': 'Michael','Employee_LN': 'Scott','Reports': [{'Employee_FN': 'Jim','Employee_LN': 'Halpert','Reports': [{'Employee_FN': 'Stanley','Employee_LN': 'Hudson'}, {'Employee_FN': 'Pam','Employee_LN': 'Beasley','Reports': [{'Employee_FN': 'Ryan','Employee_LN': 'Howard','Reports': [{'Employee_FN': 'Kelly','Employee_LN': 'Kapoor'}, {'Employee_FN': 'Meredith','Employee_LN': 'Palmer'}]}]}]}, {'Employee_FN': 'Dwight', 'Employee_LN': 'Schrute', 'Level': 1}]}]