我有一个二部图B,如下所示:
df['score3']=df.groupby('id')['score1'].transform('max')
print(df)
id score1 score2 score3
0 1 5 10 6
1 1 6 12 6
2 2 11 22 11
3 2 3 15 11
当我使用B.add_nodes_from([1, 2, 3, 4], bipartite=0)
B.add_nodes_from([5, 6, 7, 8], bipartite=1)
B.add_edges_from([(1, 8), (1, 7), (1, 6), (1,5), (2,8), (2,7), (2,6), (3,8), (3,7), (4,8)])
时,我希望边缘的顺序保持不变。相反,它们混洗出来:
B.edges()
顺序不一定是升序或降序。我希望它按照我使用[(1, 8), (1, 5), (1, 6), (1, 7), (2, 8), (2, 6), (2, 7), (3, 8), (3, 7), (4, 8)]
添加它们的顺序。因此,在这种情况下,无法使用B.add_edges_from(...)
。
答案 0 :(得分:4)
检查NetworkX的Ordered Graphs
:
g = nx.OrderedGraph()
g.add_nodes_from([1, 2, 3, 4], bipartite=0)
g.add_nodes_from([5, 6, 7, 8], bipartite=1)
g.add_edges_from([(1, 8), (1, 7), (1, 6), (1,5), (2,8), (2,7), (2,6), (3,8), (3,7), (4,8)])
print(g.edges())
输出
EdgeView([(1, 8), (1, 7), (1, 6), (1, 5), (2, 8), (2, 7), (2, 6), (3, 8), (3, 7), (4, 8)])