How to remove node in networkx?

I have a dataset that I upload as a graph for different timeframes and try to determine the relationship between them.

I want to delete all nodes that do not have edges, but I'm not sure if the command removes or removes nodes. Any idea how to do this?

+6
source share
2 answers
import networkx as nx import matplotlib.pyplot as plt G=nx.Graph() G.add_edges_from([('A','B'),('A','C'),('B','D'),('C','D')]) nx.draw(G) plt.show() 

enter image description here

 G.remove_node('B') nx.draw(G) plt.show() 

enter image description here

To remove multiple nodes, there is also a Graph.remove_nodes_from () method.

+12
source

The documentation covers it.

Graph.remove_node (n): remove node n.

Graph.remove_nodes_from (nodes): delete several nodes.

For instance:

 In : G=networkx.Graph() In : G.add_nodes_from([1,2,3]) In : G.nodes() Out: [1, 2, 3] In : G.remove_node(2) In : G.nodes() Out: [1, 3] 
+3
source

Source: https://habr.com/ru/post/899497/


All Articles