这是通过三种不同的步骤来做您想做的事情的一种方法,因此很容易遵循.
>步骤1:从边缘列表中构建networkx图形对象.
>步骤2:创建一个包含2列的数据框(对于该DF中的每一行,我们希望从e1列到e2中的实体的最短距离和路径)
>步骤3:逐行查找DF,计算最短路径和长度.将它们作为新列存储在DF中.
步骤1:建立图形并逐一添加边线
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
elist = [[('a-5', 'b-17'), ('b-17', 'c-1')], #sentence 1
[('c-1', 'a-23'), ('a-23', 'c-1')], #sentence 2
[('b-17', 'g-2'), ('g-20', 'c-1')]] #sentence 3
graph = nx.Graph()
for sentence_edges in elist:
for fromnode, tonode in sentence_edges:
graph.add_edge(fromnode, tonode)
nx.draw(graph, with_labels=True, node_color='lightblue')
步骤2:建立所需距离的资料框
#Create a data frame to store distances from the element in column e1 to e2
DF = pd.DataFrame({"e1":['c-1', 'a-23', 'c-1', 'g-2'],
"e2":['b-17', 'a-5', 'g-20', 'g-20']})
DF
步骤3:计算最短路径和长度,并存储在数据框中
这是最后一步.计算最短路径并存储它们.
pathlist, len_list = [], [] #placeholders
for row in DF.itertuples():
so, tar = row[1], row[2]
path = nx.shortest_path(graph, source=so, target=tar)
length=nx.shortest_path_length(graph,source=so, target=tar)
pathlist.append(path)
len_list.append(length)
#Add these lists as new columns in the DF
DF['length'] = len_list
DF['path'] = pathlist
产生所需的结果数据帧:
希望这对您有所帮助.