简体   繁体   中英

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 (ie Source system Charlie) and incomplete data flows (ie 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()

当前网络图输出

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.

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM