我遇到的问题非常类似于此处:
Count all subordinates who directly or indirectly reports to managers
我确实有一张表,所有员工都在一栏中,而且他们的表格也是如此。 boss_id在另一个。
employee_id boss_id receive_reports nr_reports
0 46456 175361 False 0
1 104708 29733 False 0
2 120853 41991 False 0
3 142630 171266 True 1
4 72711 198240 False 0
因此,我想要一张桌子,每个员工都可以阅读所有直接和间接报告的数量(例如,第一级所有人都为0,首先是#allemployees)。在最后一栏中,我可以轻松区分一级人员和其他人。
我想使用python并且我已经把它缠绕了很长一段时间。我确实有一个大概的想法:从第一级的所有人开始,零报告,上升一级等......
编辑1 这是我的方法(我也很想知道是否有更好的方法)
我有一份包含所有level_0员工的清单。
def sum_reports(current_level):
# pass current level of employees as list
if len(current_level) == 1:
return
else:
next_level = []
#find boss for every employees at current level and add to next level + add to report of this boss 1
for emp in current_level:
tmp_boss = data.loc[data.employee_id == emp, "boss_id"].item()
next_level.append(tmp_boss)
data.loc[(data.employee_id == tmp_boss),"nr_reports"] += 1
sum_reports(next_level)
sum_reports(level_0)
但我确实得到了错误: ValueError:只能比较带有相同标记的Series对象
编辑2 在添加.item()时,摆脱了比较错误,用tmp_boss = ...
更改了代码行但是现在我收到另一个错误,我无法追踪: ValueError:只能将大小为1的数组转换为Python标量
答案 0 :(得分:0)
我可以解决问题,执行以下操作:
如果老板已经在我的下一级别(因为有几个报告给同一个人),请添加支票
并检查boss_id是否为' 0' - 我用首席执行官替换了NaN的条目。这实际上导致了转换错误
以下是最终代码:
#a list of the bosses column as the direct_reports
direct_reports = data.boss_id.tolist()
seen = []
def sum_reports(current_level):
# pass current level of employees as list
if len(current_level) <= 1:
return
else:
next_level = []
#find boss for every employee at current level and add to next level + add 1 to report of this boss
for emp in current_level:
tmp_boss = data.loc[data.employee_id == emp, "boss_id"].item()
seen.append(tmp_boss)
#only if is the last direct report: add boss to the next_level
if direct_reports.count(tmp_boss) == seen.count(tmp_boss) and tmp_boss != 0:
next_level.append(tmp_boss)
# add reports, plus 1 and the reports sofar of the current emp
sofar = data.loc[data.employee_id == emp, "nr_reports"].item()
data.loc[(data.employee_id == tmp_boss),"nr_reports"] += (sofar+1)
sum_reports(next_level)
# level_o is a list of all employees, who don't appear in the boss column
sum_reports(level_0)