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 1,711 views

## 1 answer to this question.

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

```

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']
```

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.

• 63,720 points

## SQL - The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

I ran into the same problem due ...READ MORE

## How to lock data in excel sheet using POI, leaving cells without any data / the rest of the sheet unlocked

If the cells in a row have ...READ MORE

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

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

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

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