我有以下格式的数据

我想要生成一个新的列,它将告诉我公司层次结构中员工的级别。例如,首席执行官的级别是0。直接向首席执行官汇报的人的职级为1,低于2...and的人,以此类推。
我有每个员工的主管身份证。一个员工只能有一个主管。我有一种感觉,我可能可以使用Networkx来完成这个任务,但我无法确定如何做到这一点。
发布于 2020-03-08 06:17:17
我自己想出来的。这其实是相当微不足道的。我需要构造一个图表,其中CEO和其他员工一样是节点之一。
import networkx as nx
# Create a graph using the networx builtin function
G = nx.from_pandas_edgelist(df, 'employeeId', 'supervisorId')
# Calculate the distance of the CEO from each employee. Sepcify target = CEO
dikt_rank_of_employees = { x[0]: x[1] for x in nx.single_target_shortest_path_length(
G, target='c511b73c4ad30dde1b6d2d57ab2d4ddc') }
# Use this dictionary to create a column in the dataframe
df['rank_of_employee'] = df['employeeId'].map(dikt_rank_of_employees)结果是这样的

https://stackoverflow.com/questions/60584693
复制相似问题