我有下面的Pandas DF

enter image description here

创建DF的步骤

data=[['1','0','0','0','0'],['2','1','1','0','0|0'],['3','1','1','1','0|1'],['4','2','2','0','0|0|0'],['5','2','2','1','0|0|1'],['6','2','2','2','0|0|2'],['7','3','2','0','0|1|0'],['8','3','2','1','0|1|1'],['9','3','2','2','0|1|2'],['10','3','2','3','0|1|3'],['11','4','3','0','0|0|0|0'],['12','4','3','1','0|0|0|1'],['13','10','3','0','0|1|3|0']]
df = pd.DataFrame(data, columns=['eid','m_eid','level','path_variable','complete_path'])
df=df.drop('complete_path',axis=1)

这里:

eid =员工ID

m_eid =管理员ID

level =组织中的级别(0为最高老板)

path_variable =根据级别分配给员工的增量编号,该编号 for each 经理重置(例如:eid [4,5,6,7,8,9,10]属于同一级别2,但eid [4,5,6]具有相同的经理(m_eid = 2),因此path_variable为0,1,2,而eid [7,8,9,10]具有不同的经理(m_eid = 3),因此path_variable从0重新启动)

我想创建一个新的列,显示完整的路径,直到级别0 for each Eid.如下图所示:

output

完整路径是路径_variable直到0级(顶部凸台)的连接.

从根 node 到边缘 node 的路径.为了前任.让我们go 开斋节10

enter image description here

直接管理人员之间可能存在级别 skip .由于性能限制,我试图避免使用iterrows().

推荐答案

IIUC,你可以用networkx构建一个有向图,然后找到每个 node 和'0'之间的shortest_path,然后用它映射path_variable:

import networkx as nx

G = nx.from_pandas_edgelist(df, source='m_eid', target='eid',
                            create_using=nx.DiGraph)

s = df.set_index('eid')['path_variable']

mapper = {n: '|'.join(s.get(x, '') for x in 
                      nx.shortest_path(G, source='0',
                                       target=n)[1:])
          for n in df['eid'].unique()
         }
df['complete_path'] = df['eid'].map(mapper)

输出:

   eid m_eid level path_variable complete_path
0    1     0     0             0             0
1    2     1     1             0           0|0
2    3     1     1             1           0|1
3    4     2     2             0         0|0|0
4    5     2     2             1         0|0|1
5    6     2     2             2         0|0|2
6    7     3     2             0         0|1|0
7    8     3     2             1         0|1|1
8    9     3     2             2         0|1|2
9   10     3     2             3         0|1|3
10  11     4     3             0       0|0|0|0
11  12     4     3             1       0|0|0|1
12  13    10     3             0       0|1|3|0

图表:

organization graph, networkx graphviz

如果eid中已经有了唯一值,则可以避免映射器并使用用途:

df['complete_path'] = ['|'.join(s.get(x, '') for x in 
                       nx.shortest_path(G, source=n,
                                        target='0')[-2::-1])
                       for n in df['eid']]

为了更容易理解,这里有一个更classic 的路径, node id(而不是path_variables):

mapper = {n: '|'.join(nx.shortest_path(G, source='0',
                                       target=n)[1:])
          for n in df['eid'].unique()
         }
df['complete_path'] = df['eid'].map(mapper)

输出:

   eid m_eid level path_variable complete_path
0    1     0     0             0             1
1    2     1     1             0           1|2
2    3     1     1             1           1|3
3    4     2     2             0         1|2|4
4    5     2     2             1         1|2|5
5    6     2     2             2         1|2|6
6    7     3     2             0         1|3|7
7    8     3     2             1         1|3|8
8    9     3     2             2         1|3|9
9   10     3     2             3        1|3|10
10  11     4     3             0      1|2|4|11
11  12     4     3             1      1|2|4|12
12  13    10     3             0     1|3|10|13

Python相关问答推荐

使用pandas、matplotlib和Yearbox绘制时显示错误的年份

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

处理(潜在)不断增长的任务队列的并行/并行方法

如何在polars(pythonapi)中解构嵌套 struct ?

如何在Python数据框架中加速序列的符号化

Stacked bar chart from billrame

从spaCy的句子中提取日期

在ubuntu上安装dlib时出错

如何在Python中找到线性依赖mod 2

无法连接到Keycloat服务器

如何指定列数据类型

OpenCV轮廓.很难找到给定图像的所需轮廓

pysnmp—lextudio使用next()和getCmd()生成器导致TypeError:tuple对象不是迭代器''

用SymPy在Python中求解指数函数

将链中的矩阵乘法应用于多组值

ModuleNotFoundError:Python中没有名为google的模块''

修改.pdb文件中的值并另存为新的

Polars定制函数返回多列

删除另一个div中的特定div容器

如何批量训练样本大小为奇数的神经网络?