I have a dataset that I'm uploading as a graph for various timeframes and trying to figure relationships between them.
I want to delete all the nodes that do not have edges but I'm not sure the command to remove or delete nodes. Any idea how to do this?
Remove node n. Removes the node n and all adjacent edges. Attempting to remove a non-existent node will raise an exception.
H = rmnode( G , nodeIDs ) removes the nodes specified by nodeIDs from graph G . Any edges incident upon the nodes in nodeIDs are also removed.
Node attributes Note that adding a node to G.nodes does not add it to the graph, use G.add_node() to add new nodes.
remove_edges_from : remove all the edges from a graph. clear : remove all the nodes and edges from a graph.
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()
G.remove_node('B')
nx.draw(G)
plt.show()
To remove multiple nodes, there is also the Graph.remove_nodes_from() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With