Search code examples
pythongraphnodesnetworkxedges

Can Networkx read nodes and edges from different files?


I have a csv file containing this nodes, with the associated coordinates for each like so:

    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

And for each of those nodes I have another file with edges, that represents the distance between each connected node, like this:

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

So, what can I do to add the nodes and their corresponding edges to the same graph?

I tried this, but it doesn't work:

    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() 

I'm new to Python and I need this as part of the code for my master thesis. I'm using python 3.6 but I have also installed the 2.7 version. Can you help me make this work?


Solution

  • Networkx has some utility functions which could make your life a little easier. You could use nx.from_pandas_dataframe to generate a Graph directly from your edges DataFrame:

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

    and then you can add node attributes by converting the nodes DataFrame to a list of dicts, then loading them into the Graph, G with G.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))
    

    prints (for 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}})
    

    and (for G.edges(data=True)):

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