Skip to content Skip to sidebar Skip to footer

Adjacency Matrix From Pandas Edgelist Dataframe For Undirected Graphs

data1 = { 'node1': [1,1,1,2], 'node2': [2,3,5,4], 'weight': [1,1,1,1], } df1 = pd.DataFrame(data1, columns = ['node1','node2','weight']) I want to create an adjacency ma

Solution 1:

Using networkx.....

data1 = { 'node1': [1,1,1,2],
 'node2': [2,3,5,4],
 'weight': [1,1,1,1], }
df1 = pd.DataFrame(data1, columns = ['node1','node2','weight'])       
G=nx.from_pandas_dataframe(df1,'node1','node2','weight')

Adjtraining = nx.adjacency_matrix(G)

print Adjtraining.todense()

output

[[0 1 1 0 1]
 [1 0 0 1 0]
 [1 0 0 0 0]
 [0 1 0 0 0]
 [1 0 0 0 0]]

Post a Comment for "Adjacency Matrix From Pandas Edgelist Dataframe For Undirected Graphs"