Search code examples
pythondictionaryhierarchy

Setup up a hierarchy from dicts


I have a single CSV file of employees where I have employee data including the name, boss, department id and department name. By reading that CSV file, I have created those 2 dict structures:

dep = {}
dep[1] = {'name': 'Sales', 'parent': None}
dep[2] = {'name': 'National Sales', 'parent': None}
dep[3] = {'name': 'International Sales', 'parent': None}
dep[4] = {'name': 'IT', 'parent': None}
dep[5] = {'name': 'Development', 'parent': None}
dep[6] = {'name': 'Support', 'parent': None}
dep[7] = {'name': 'Helpdesk', 'parent': None}
dep[8] = {'name': 'Desktop support', 'parent': None}
dep[9] = {'name': 'CEO', 'parent': None}

emp = {}
emp[1] = {'name': 'John', 'boss': None, 'dep': 9}
emp[2] = {'name': 'Jane', 'boss': 1, 'dep': 1}
emp[3] = {'name': 'Bob', 'boss': 2, 'dep': 1}
emp[4] = {'name': 'Clara', 'boss': 2, 'dep': 2}
emp[5] = {'name': 'George', 'boss': 3, 'dep': 2}
emp[6] = {'name': 'Steve', 'boss': 2, 'dep': 3}
emp[7] = {'name': 'Joe', 'boss': 1, 'dep': 4}
emp[8] = {'name': 'Peter', 'boss': 7, 'dep': 5}
emp[9] = {'name': 'Silvia', 'boss': 7, 'dep': 6}
emp[10] = {'name': 'Mike', 'boss': 9, 'dep': 7}
emp[11] = {'name': 'Lukas', 'boss': 10, 'dep': 7}
emp[12] = {'name': 'Attila', 'boss': 7, 'dep': 8}
emp[13] = {'name': 'Eva', 'boss': 12, 'dep': 8}

Out of this I have 2 tasks:

  1. Create a hierarchy of departments. (basically fill the value of the parent key)
  2. Display (list) all the departments and employees for a boss

Expected result for the point #2 would be (everybody working in sales):

employees = {1: (2, 3, 4, 5, 6)}

for everybody working in National Sales:

employees = {4: (5)}

and for everybody working in International Sales (Steve is the only one, nobody is working for him)):

employees = {6: None}

How to achieve this in a performant manner (I have to handle several thousands employees)?

EDIT: This a (simplified) CSV file structure:

id;name;boss;dep_id;dep_name
    1;John;;9;CEO
    2;Jane;1;1;Sales
    3;Bob;2;1;Sales
    4;Clara;2;2;National Sales
    5;George;3;2;National Sales
    6;Steve;2;3;International Sales
    7;Joe;1;4;IT
    8;Peter;7;5;Development
    9;Silvia;7;6;Support
    10;Mike;9;7;Helpdesk
    11;Lukas;10;7;Helpdesk
    12;Attila;7;8;Desktop support
    13;Eva;12;8;Desktop support

Solution

  • As suggested in the comments, here is a solution using pandas. The file is mocked using your example data, and it should be plenty fast for only a few thousand entries.

    from StringIO import StringIO
    import pandas as pd
    
    f = StringIO("""
    id;name;boss;dep_id;dep_name
    1;John;1;9;CEO
    2;Jane;1;1;Sales
    3;Bob;2;1;Sales
    4;Clara;2;2;National Sales
    5;George;3;2;National Sales
    6;Steve;2;3;International Sales
    7;Joe;1;4;IT
    8;Peter;7;5;Development
    9;Silvia;7;6;Support
    10;Mike;9;7;Helpdesk
    11;Lukas;10;7;Helpdesk
    12;Attila;7;8;Desktop support
    13;Eva;12;8;Desktop support
    """)
    
    # load data
    employees = pd.read_csv(f, sep=';', index_col=0)
    
    ### print a department ###
    # Filter by department and print the names
    print employees[employees.dep_id == 7].name
    
    ### build org hierarchy ###
    # keep only one entry per department (assumes they share a boss)
    org = employees[['boss', 'dep_id']].drop_duplicates('dep_id')
    # follow the boss id to their department id
    # note: the CEO is his own boss, to avoid special casing
    org['parent'] = org.dep_id.loc[org['boss']].values
    # reindex by department id, and keep only the parent column
    # note: the index is like your dictionary key, access is optimized
    org = org.set_index('dep_id')[['parent']]
    print org