0

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_new, source = 'SourceNode', target='target' , edge_attr= 'Weight')

I error when Ia m trying this code.. I am very new to visualization in pandas. I appreciate any help.

4
  • 1
    What is the error you are getting. If you don't share how do you expect anyone to know? Commented Jan 7, 2023 at 1:02
  • Have you installed networkx package, try running pip show networkx to check Commented Jan 7, 2023 at 1:09
  • I was getting no module named networkx error. Commented Jan 10, 2023 at 22:02
  • I reinstalled the networkx and it now it seemed worked. Commented Jan 10, 2023 at 22:02

1 Answer 1

3

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

enter image description here


Question 2

In order to show nodes title and edges weights on the plotted network, only few lines I have added in your code:

# Creating Undirected graph            
G = nx.from_pandas_edgelist(df_new, source='deid_billing_prov_npi', target='deid_referring_prov_npi', edge_attr='claim_count')

# Adding layout of your networkx Graph
pos = nx.kamada_kawai_layout(G)

# Drawing that graph with nodes titles only
nx.draw(G, pos=pos, with_labels=True)

# Creating labels for edges weights column
edge_width = {e: G.edges[e]['claim_count'] for e in G.edges}

# Drawing complete network with edges weights and nodes titles
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_width)

Since I don't have your sample data for this question, Based on the data for your first queston, this is what your undirected labeled graph looks like:

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks that worked. Is there anyway I can add the node titles and weight of edges?
I am adding this : G= nx.from_pandas_edgelist(df_new, source = 'deid_billing_prov_npi', target='deid_referring_prov_npi' , edge_attr= 'claim_count') edges = G.edges.data() pos = nx.kamada_kawai_layout(G) edge_width = [e[2]['claim_count'] for e in edges] nx.draw(G, pos=pos, with_labels=True) # draw nodes (and edges!) nx.draw_networkx_edges(G, pos=pos, width=edge_width)
@Shapa, I have updated my answer, very few changes are required in your code. Since these are the basic of visualization in networkX, please go through the documentation of networkX or any YouTube tutorial, Moreover if my provided code solves your problem then do marked my answer. Thank You!
Is it possible to remove the edge name and replace the edge weight with stronger bigger line ? Do you know how to color only Source Node color?
@Shapa, for edges weight link and for coloring source node you have to think on your own using this approach link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.