经理和员工ID的python层次结构 [英] python hierarchy from manager and employee id
问题描述
我有一个包含两列的csv:employee id 'eid'
和经理的employee id 'mid'
.试图获取python代码,此代码将为每位员工添加一列,以显示从经理到CEO的经理的员工ID. CEO的雇员ID为1.最终,我想将结果写回csv.
I have a csv with two columns: employee id 'eid'
and manager's employee id 'mid'
. Trying to get python code that will, for each employee, add columns showing employee id's of the manager all the way to CEO. CEO has a an employee id of 1. Ultimately I want to write the result back to a csv.
所以数据看起来像:
eid, mid
111, 112
113, 112
112, 114
114, 115
115, 1
我期望输出看起来像这样.请注意,虽然没有一个员工拥有超过4个级别的经理,但是我还想学习动态命名列的python.
I am expecting output that looks like this. Note that while no employee will have more than 4 levels of managers, but i would like to also learn python that names columns dynamically.
eid, mid, l2mid l3mid l4mid
111, 112, 114, 115, 1
113, 112, 114, 115, 1
112, 114, 115, 1
114, 115, 1
115, 1
我对编码非常陌生,他试图自学,但不断陷入困境.我的问题:
1)我试图使用for语句,该语句在给定行中包含mid
,然后发现该经理的经理,依此类推,直到我到达CEO.我一直在尝试以下方法:
I am very new to coding, and trying to teach myself but keep getting stuck. My questions:
1) I was trying to use a for statement that took mid
in a given row, then found that that manager's manager, and so on, until i reached the CEO. I have been trying along these lines:
df = pd.read_csv('employee.csv')
if mid =! 1
for i in df:
df.['l2mid'] = df.loc[df.eid == [i], [mid]]
也许我正在倒退,我应该尝试按经理对所有员工进行分组吗?该代码有何不同?
Maybe I'm approaching this backwards and I should try grouping all employees by manager? How would that code be different?
I have seen solutions in C# and sql, and i've seen solutions that build trees and json. I really appreciate any help and encouragement.
Update: next step was to add a country column - see: entry here
推荐答案
我相信有更好的解决方案,但是可以.我把零填满了.
I believe there is a better solution, but this works. I filled empty with zeros.
a = []
for index, row in df.iterrows():
res = df[df['eid']==row['mid']]['mid'].values
a.append(0 if not res else res[0])
df['l2mid'] = a
a = []
for index, row in df.iterrows():
res = df[df['eid']==row['l2mid']]['mid'].values
a.append(0 if not res else res[0])
df['l3mid'] = a
a = []
for index, row in df.iterrows():
res = df[df['eid']==row['l3mid']]['mid'].values
a.append(0 if not res else res[0])
df['l4mid'] = a
df
# output :
# eid mid l2mid l3mid l4mid
# 0 111 112 114 115 1
# 1 113 112 114 115 1
# 2 112 114 115 1 0
# 3 114 115 1 0 0
# 4 115 1 0 0 0
您可以为例程定义一个函数.
You can define a function for routines.
def search_manager(target_column, new_column):
a = []
for index, row in df.iterrows():
res = df[df['eid']==row[target_column]]['mid'].values
a.append(0 if not res else res[0])
df[new_column] = a
search_manager('mid', 'l2mid')
search_manager('l2mid', 'l3mid')
search_manager('l3mid', 'l4mid')
这篇关于经理和员工ID的python层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!