【问题标题】:Can Networkx read nodes and edges from different files?Networkx 可以从不同的文件中读取节点和边吗?
【发布时间】:2018-06-22 21:27:57
【问题描述】:

我有一个包含这些节点的 csv 文件,每个节点都有相关的坐标,如下所示:

    node x y
    A1  67.8    15.53
    A2  108.74  15.53
    B1  67.8    25.33
    B2  108.74  25.33
    C1  67.8    30.22
    C2  108.74  30.22
    D1  67.8    37.99
    D2  108.74  37.99
    E1  67.8    43.84

对于这些节点中的每一个,我都有另一个带有边的文件,它表示每个连接节点之间的距离,如下所示:

   node1 node2 distance
   A1 A2 40.90
   A1 B1 9.8
   A2 B2 9.8
   B1 A1 9.8
   ...

那么,我该怎么做才能将节点及其对应的边添加到同一个图中?

我试过了,但它不起作用:

    import pandas as pd 
    import networkx as nx 
    import matplotlib.pyplot as plt
    import numpy

    nodes = pd.read_csv('nodes.csv')
    print nodes

    G = nx.Graph()

    for row in nodes.iterrows():
      G.add_node(row[1][0], x=row[1][2],y=row[1][3])

     edgelist = pd.read_csv('edges.csv')

     print edgelist


     for i, elrow in edgelist.iterrows():
     G.add_edge(elrow.node1,elrow.node2,weight=elrow.distance)

     G.nodes(data=True)

     nx.draw(G)
     plt.show() 

我是 Python 新手,我需要将它作为我硕士论文代码的一部分。我使用的是 python 3.6,但我也安装了 2.7 版本。你能帮我完成这项工作吗?

【问题讨论】:

    标签: python graph nodes networkx edges


    【解决方案1】:

    Networkx 有一些utility functions 可以让您的生活更轻松一些。 您可以使用nx.from_pandas_dataframe 直接从您的edges DataFrame 生成图表:

    edges = pd.read_csv('edges.csv', sep='\s+')
    G = nx.from_pandas_dataframe(edges, 'node1', 'node2', ['distance'])
    

    然后您可以通过将nodes DataFrame 转换为字典列表来添加节点属性,然后将它们加载到图表中,GG.add_nodes_from(data)

    nodes = pd.read_csv('nodes.csv', sep='\s+')
    data = nodes.set_index('node').to_dict('index').items()
    G.add_nodes_from(data)
    

    import pandas as pd 
    import networkx as nx 
    import matplotlib.pyplot as plt
    import numpy as np
    
    edges = pd.read_csv('edges.csv', sep='\s+')
    G = nx.from_pandas_dataframe(edges, 'node1', 'node2', ['distance'])
    
    nodes = pd.read_csv('nodes.csv', sep='\s+')
    data = nodes.set_index('node').to_dict('index').items()
    G.add_nodes_from(data)
    print(G.nodes(data=True))
    print(G.edges(data=True))
    

    打印(用于G.nodes(data=True)):

    NodeDataView({'D1': {'y': 37.990000000000002, 'x': 67.799999999999997}, 'A1': {'y': 15.529999999999999, 'x': 67.799999999999997}, 'C2': {'y': 30.219999999999999, 'x': 108.73999999999999}, 'B2': {'y': 25.329999999999998, 'x': 108.73999999999999}, 'D2': {'y': 37.990000000000002, 'x': 108.73999999999999}, 'C1': {'y': 30.219999999999999, 'x': 67.799999999999997}, 'A2': {'y': 15.529999999999999, 'x': 108.73999999999999}, 'E1': {'y': 43.840000000000003, 'x': 67.799999999999997}, 'B1': {'y': 25.329999999999998, 'x': 67.799999999999997}})
    

    和(对于G.edges(data=True)):

    EdgeDataView([('A1', 'A2', {'distance': 40.9}), ('A1', 'B1', {'distance': 9.8}), ('B2', 'A2', {'distance': 9.8})])
    

    【讨论】:

    • 非常感谢您的帮助,这正是我想要的。
    • @AlvaroSilva 您是否也可以接受您所说的解决了您的问题的答案? (点击答案左上角的green tick)。
    • @Bonlenfum 当然,我只是不知道如何接受它。
    • 没问题 - 欢迎访问该网站。点击“upvote”和/或接受解决问题的答案可以让有相同问题的其他人更容易选择要阅读的答案!
    • networkx 2中不存在该方法。使用G = nx.from_pandas_edgelist(edges, 'src', 'tgt', edge_attr=True)
    【解决方案2】:

    from_pandas_dataframe 不适用于 networkx v2。

    我在这里找到了正确的方法名称:AttributeError: module 'networkx' has no attribute 'from_pandas_dataframe'

    对我来说有效的是:

    G = nx.from_pandas_edgelist(edges, 'src', 'tgt', edge_attr=True)
    

    在那之后,我可以使用其余已接受的答案:

    data = nodedf.set_index('id').to_dict('index').items()
    G.add_nodes_from(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-24
      • 2013-08-02
      • 2018-08-08
      • 1970-01-01
      • 2022-06-14
      • 2013-02-26
      • 2013-03-18
      • 1970-01-01
      相关资源
      最近更新 更多