Search code examples
pythonexcelnetworkx

How to represent the data of an excel file into a directed graph? Python


I have downloaded California road network dataset from Stanford Network Analysis Project. The data is a text file which can be converted to an excel file with two columns. The first is for the start nodes, and the second column is for the end nodes.

# FromNodeId    ToNodeId
       0           1
       0           2
       0          469
       1           0
       1           6
       1          385
       2           0
       2           3
      469          0
      469         380
      469        37415
       6           1
       6           5
      385          1
      385         384
      385         386
       3           2
       3           4
       3          419
       3          422

Now, how can I want to convert this data into an adjacency matrix or any other object for analysing the graph? For example for calculating degree distribution, clustering coefficients, etc. I will be grateful for any help on how to represent this data into a graph using python and related libraries.


Solution

  • It seems like your're looking for degree_distrubition and average_clustering in :

    #pip install networkx
    import networkx as nx
    
    G = nx.read_edgelist("roadNet-CA.txt", nodetype=int, create_using=nx.DiGraph())
    
    degree_distribution = nx.degree_histogram(G)
    #[0, 8, 0, 1, 3, 1, 2]
    clustering_coefficient = nx.average_clustering(G)
    #0.0
    

    enter image description here