gpt4 book ai didi

python - 确定每天的员工级别

转载 作者:行者123 更新时间:2023-12-02 00:59:46 25 4
gpt4 key购买 nike

我试图通过每一天来确定员工在组织中的深度。最高层(CEO)是第 1 级,他们的直接下属是第 2 级,等等。

我能够计算无日期,但无法计算每天的员工级别。这是我所做的:

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)

current output

expected output

最佳答案

您可以使用 groupby 并每天传递 dct 字典:

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

它给出了预期的结果:

        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

关于python - 确定每天的员工级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60936186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com