user12907213
user12907213

Reputation:

Nodes and edges columns in dataframe to build a network

I am facing difficulties in reproduce visually the following data:

     Nodes                            Edges
0   342                              [342]
1   7564                             [7564]
2   21                               [21]
3   43                               [43, 24,4346]
4   4346                             [4346,43]
... ... ...
74  24                               [24,43]

This dataset includes nodes and information on edges between nodes. Specifically, in the brackets there are edges between a node and another node (or just itself). For instance:

node 43 has three edges with itself, node 24 and node 4346 respectively. Node 24 has only two edges, one with itself and another with node 43; same for node 4346. Node 342, node 7564 and node 21 have only edges with themselves. I would like to have a chart/network which can show these relationships. Unfortunately I am not able to create edges using the information within the brackets (in pandas column), but only nodes. I am using Networkx. I hope you can help me and explain me how to create links between nodes, considering the relationships described in Edges column.

Thank you

Upvotes: 0

Views: 883

Answers (1)

BENY
BENY

Reputation: 323226

we need first explode the list column then create the network with networkx

import networkx as nx
df = df.explode('Edges')
G = nx.from_pandas_edgelist(df, 'Nodes', 'Edges')

Upvotes: 1

Related Questions