I have a large dataframe(df) with Weight ,Source Node, target columns.
SourceNode | target | Weight |
---|---|---|
176890 | 657826 | 201 |
136578 | 589231 | 300 |
143873 | 457139 | 50 |
134589 | 892147 | 550 |
198345 | 678931 | 350 |
112443 | 525188 | 600 |
336128 | 689313 | 1500 |
Source node and target column are object and weight is int data type.
I am trying to create a network graph which shows the connectivity from source node to target column (unidirect) and the edge need to reflect the weight value( like by stronger bigger connection).
This is the code I am using I am getting error not showing anything for me.
import networkx as nx
G = nx.from_pandas_edgelist(df, source='Source Node', target='target', edge_attr='Weight')
I appreciate any feedback, I am new to python.
2
First of all, It is SourceNode
column not Source Node
.
Also this is what your code must looks like in order to draw the undirected graph from that dataframe
.
# Loading networkX library
import networkx as nx
# Loading Pandas library
import pandas as pd
# Loading your CSV file dataset
df = pd.read_csv('yourdata.csv')
# Creating Undirected graph
G = nx.from_pandas_edgelist(df, source='SourceNode', target='target', edge_attr='Weight')
# Drawing that graph
nx.draw(G, node_size=40)
Output