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

0 votes

I have downloaded the California road network dataset from Stanford Network Analysis Project. The data is a text file that 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

How would I turn this data into an adjacency matrix or any other object for graph analysis? as in the computation of clustering coefficients, degree distribution, etc? Any assistance utilizing Python and related packages to represent this data as a graph would be greatly appreciated.

Apr 9, 2023 in Others by Kithuzzz
• 38,000 points
1,969 views

1 answer to this question.

0 votes

The pandas and networkx packages in Python can be used to transform the data from an XLS file into a directed graph. the subsequent actions:

Read the Excel file using pandas:

import pandas as pd

df = pd.read_excel('filename.xlsx')

Create a directed graph using networkx:

import networkx as nx

G = nx.DiGraph()
Iterate over the rows of the dataframe and add nodes and edges to the graph:


for index, row in df.iterrows():
    source_node = row['Source']
    target_node = row['Target']
    weight = row['Weight']
    G.add_edge(source_node, target_node, weight=weight)

The Excel file in this example is assumed to have three columns: Source, Target, and Weight. Weight denotes the weight/edge cost between two nodes with the labels Source and Target.

After that, you may use matplotlib or another library of your choosing to see the graph:

import matplotlib.pyplot as plt

pos = nx.spring_layout(G)

nx.draw_networkx_nodes(G, pos, node_size=500)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos)

plt.show()

This code will create a directed graph using the data in the Excel file and visualize it on the screen.

answered Apr 9, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
0 answers

how to list the contents of a asset into an event

May 29, 2019 in Others by anonymous
658 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to stick an embedded document in a specific cell of an excel

Solution Select the documents (you can use the ...READ MORE

answered Oct 18, 2022 in Others by narikkadan
• 63,600 points
701 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

How can I store the data of an open excel workbook in a collection using BluePrism?

To do what you want is like ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,600 points
1,361 views
0 votes
1 answer

How to divide data in excel into 4 columns whose sum is almost equal to 1/4 of the sum of all values/

5049 is the sum of all numbers, ...READ MORE

answered Feb 7, 2023 in Others by narikkadan
• 63,600 points
791 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP