0

Is there a better way to create a listing of employees that belong to a single leaders' organization when I only have a listing of employees and their managers?

My current approach is to find in FULL (dataframe with all employees and their managers) the employees who report to that executive (TEMP1), append TEMP1 to a running list (SMITH_STAFF), then iterate over TEMP1 to find their direct reports in FULL (saved to TEMP2), save TEMP2 to SMITH_STAFF, etc.

My approach works, but it is quite kooky. There has to be a better way, but all my searches and trying for loops, etc. did not work. I can't find a more elegant way.

The FULL dataframe contains 'Email Address' and 'Manager Email Address' for all employees. Example:

Email Address Mgr Email Address employee1 johnsmith employee2 employee1 employee3 employee2 employee4 tomhamks

SMITH_STAFF = pd.DataFrame()
TEMP1 = FULL.loc[FULL['Mgr Email Address']=="johnsmith@company.com"]
SMITH_STAFF = SMITH_STAFF.append(TEMP1)

TEMP2 = pd.DataFrame()
for index, row in TEMP1.iterrows():
    TEMP2 = TEMP2.append(FULL.loc[FULL['Mgr Email Address']==row["Email 
Address"]])
SMITH_STAFF = SMITH_STAFF.append(TEMP2)

TEMP3 = pd.DataFrame()
for index, row in TEMP2.iterrows():
    TEMP3 = TEMP3.append(FULL.loc[FULL['Mgr Email Address']==row["Email 
Address"]])
SMITH_STAFF = SMITH_STAFF.append(TEMP3)

I expect the smith staff file to contain the employee and manager email addresses for all employees that ultimately report up to JohnSmith. In this example, that would be employee1, employee2, and employee3

richcru
  • 3
  • 1
  • 3
  • Is the leader at the top level or might they also have a manager? If top level, a (fairly efficient?) recursive function could get the top-level leader for each employee. Then it would be a simple selection. – Joe P May 31 '19 at 22:34
  • Rather than iterating, you might be able to find a simpler solution with groupby and list, as in [this question](https://stackoverflow.com/questions/22219004/grouping-rows-in-list-in-pandas-groupby) – G. Anderson May 31 '19 at 22:35
  • Is your 'Example:' for the FULL dataframe supposed to be formatted? I can't follow it. – Joe P May 31 '19 at 22:35
  • I can't vouch for it, but some quick googling turned up the following: https://stackoverflow.com/questions/46521390/getting-all-descendants-of-a-parent-from-a-pandas-dataframe-parent-child-table – Mike May 31 '19 at 23:05
  • They also have a manager. The manager is listed for each employee, which means I need to be able to trace the managers up to the right leader. – richcru Jun 01 '19 at 15:58
  • Thanks for the tips on the groupby and getting the descendents from a parent. My search terms weren't that great I guess. I'll check it out. – richcru Jun 01 '19 at 15:59
  • This routine worked perfectly: stackoverflow.com/questions/46521390/ – richcru Jun 02 '19 at 16:19

0 Answers0