pythonnewbie :
I'm trying to determine an employee's depth in the organization by each day. The top level (CEO) is Level 1, their direct reports are level 2, etc.
I was able to calculate w/o date but having trouble calculating employee level for each day. Here's what I've done:
import pandas as pd
# setup df
df = pd.DataFrame({'date': ['3/29/2020', '3/29/2020', '3/29/2020', '3/30/2020', '3/30/2020', '3/30/2020', '3/30/2020'],
'empid': [1, 2, 3, 1, 2, 3, 4], 'mgrid': [0, 1, 2, 0, 1, 1, 2]})
# create dictionary
dct = dict(zip(df['empid'].values, df['mgrid'].values))
# function to create employee level
def level(empid):
top = 1
while (dct[empid]) != 0:
top += 1
empid = dct[empid]
return top
#apply level
df['level'] = df['empid'].apply(level)
Serge Ballesta :
You could use a groupby and pass the dct
dict per day:
def level(empid, dct):
top = 1
while (dct[empid]) != 0:
top += 1
empid = dct[empid]
return top
def dflevel(x):
dct = dict(zip(x['empid'].values, x['mgrid'].values))
return x['empid'].apply(level, args=[dct])
df['level'] = df.groupby('date').apply(dflevel).values
It gives as expected:
date empid mgrid level
0 3/29/2020 1 0 1
1 3/29/2020 2 1 2
2 3/29/2020 3 2 3
3 3/30/2020 1 0 1
4 3/30/2020 2 1 2
5 3/30/2020 3 1 2
6 3/30/2020 4 2 3
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=386804&siteId=1