You can create an adjacency matrix in Networkx using the nx.to_numpy_matrix()
or nx.to_pandas_adjacency()
functions as follows:
main.py260 chars16 lines
This will output:
main.py61 chars5 lines
If you want to create a pandas DataFrame from the adjacency matrix, use nx.to_pandas_adjacency()
function:
main.py160 chars7 lines
This will output:
main.py110 chars6 lines
Note: In this example, we have created a directed graph. If you have an undirected graph, you can use nx.to_numpy_matrix(G, nonedge=0)
to get an adjacency matrix with zeros in non-existent edges.
gistlibby LogSnag