Search code examples
pythonpandasnetworkx

Networkx graph from pandas with nodes with edges that don't connect other nodes and nodes with no edges


I am working to develop a network graph that shows the data flow between systems. The network is still being built out, but the goal is to visualize the data flows. In the example below, you can see the Source system, Target system and Payload item. The nulls are purposeful, as portions of the network are still under construction. I would like to be able to capture disconnected nodes (i.e. Source system Charlie) and incomplete data flows (i.e. Source system Foxtrot and Target system Delta).

Source Payload Target
Alpha foo Bravo
Bravo bar Charlie
cat Delta
Echo dog Alpha
Foxtrot hat

What I have developed so far creates a node for null as an empty string. Sample code

df = pd.read_csv('X.csv',keep_default_na=False)

G = nx.from_pandas_edgelist(df=df, source='SOURCE',target='TARGET', edge_attr=True, create_using=nx.MultiDiGraph())

nx.draw(G, with_labels=True)
plt.show()

Current Network Graph Output

I would like Delta and Foxtrot to be disconnected nodes. I would also like the Edges to be labeled with the Payload but that's a nice to have.


Solution

  • You can remove the empty string node from the graph using

    G.remove_node('')

    This also removes the edges from the empty string node.

    Now the nodes Delta and Foxtrot are disconnected.

    enter image description here