I wanted to know how I can change a single node name of a node of a digraph. I am new to networkx and could only find answers on how to change all node names.
In my case I am iterating over a graph A to create graph B. p
and c
are nodes of graph A. The edge (p,c) of graph A contains data I want to add to the node p
of B. However, when I am adding the edge data from graph A to the already existing node p
of graph B, I would like to update the name of p
to be equal to the name of c
so I am able to reference it again for the next edge of graph A because it then is the edge (c,x) and I can use the c
to reference it again...
The relevant part of my code looks like this
new_stages = A.in_edge(c, data='stages')
stages = B.node[p]['stages']
stages.append(new_stages)
<<Update node p to have name of c??>>
B.add_node(p, stages=new_stage_set)
Any help is appreciated, thanks!
You have nx.relabel_nodes
for this. Here's a simple use case:
G = nx.from_edgelist([('a','b'), ('f','g')])
mapping = {'b':'c'}
G = nx.relabel_nodes(G, mapping)
G.edges()
# EdgeView([('a', 'c'), ('f', 'g')])
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