Reputation: 135
Suppose I have this dataframe df
that contains 3794 rows x 2 columns, where column a-number
represents nodes with directed edges to nodes in b-number
:
a_number b_number
0 0123456789343 0123456789991
1 0123456789343 0123456789633
2 0123456789343 0123456789633
3 0123456789343 0123456789628
4 0123456789343 0123456789633
... ... ...
3789 0123456789697 0123456789916
3790 0123456789697 0123456789886
3791 0123456789697 0123456789572
3792 0123456789697 0123456789884
3793 0123456789697 0123456789125
3794 rows × 2 columns
Additional information:
len(df['a_number'].unique())
>>> 18
len(df['b_number'].unique())
>>>1145
I am trying to generate an image representation of the graph. Here's code to apply networkx:
import networkx as nx
G = nx.DiGraph()
for i, (x, y) in df.iterrows():
G.add_node(x)
G.add_node(y)
G.add_edge(x,y)
nx.draw(G, with_labels = True, font_size=14 , node_size=2000)
The thing is what if I want to visualizing the graphs only from the same value in the column a_number
to the b_number
column, for example:
The 1st graph
a_number b_number
0 0123456789343 0123456789991
1 0123456789343 0123456789633
2 0123456789343 0123456789633
3 0123456789343 0123456789628
4 0123456789343 0123456789633
The next graph
a_number b_number
3789 0123456789697 0123456789916
3790 0123456789697 0123456789886
3791 0123456789697 0123456789572
3792 0123456789697 0123456789884
3793 0123456789697 0123456789125
Please advise. What can I do on the code? thank you.
Upvotes: 0
Views: 73
Reputation: 2696
You could iterate through all values of 'a_number'
:
import networkx as nx
import pandas as pd
a_values = df['a_number'].unique()
for a_value in a_values:
df_temp = df[df['a_number'] == a_value]
G = nx.DiGraph()
for i, (x, y) in df_temp.iterrows():
G.add_node(x)
G.add_node(y)
G.add_edge(x,y)
nx.draw(G, with_labels = True, font_size=14 , node_size=2000)
--- save and close plot ---
Upvotes: 1