我有数据:
issue | father | son |
---|---|---|
1 | 33 | 34 |
1 | 34 | 35 |
2 | 33 | 34 |
以下是Python中的脚本,它按层次 struct 检索表:
G = nx.from_pandas_edgelist(df, source='father', target='son', create_using=nx.DiGraph ,edge_attr=True)
print(G.out_degree())
roots = [v for v, d in G.in_degree() if d == 0]
leaves = [v for v, d in G.out_degree() if d == 0]
all_paths = []
for root in roots:
for leaf in leaves:
paths = nx.all_simple_paths(G, root, leaf)
all_paths.extend(paths)
for node in nx.nodes_with_selfloops(G):
all_paths.append([node, node])
df_h=pd.DataFrame(sorted(all_paths)).add_prefix('Value_Depend_On_ID_').fillna('')
df_h=df_h.replace('',np.nan, regex=True)
df_h.astype('Int64')
输出:
Value_Depend_On_ID_0 | Value_Depend_On_ID_1 | Value_Depend_On_ID_2 |
---|---|---|
33 | 34 | 35 |
所需的输出:
Value_Depend_On_ID_0 | Value_Depend_On_ID_1 | Value_Depend_On_ID_2 | issue |
---|---|---|---|
33 | 34 | 35 | 1 |
33 | 34 | null | 2 |
如何添加问题?