Bibek Mishra
Bibek Mishra

Reputation: 190

Number of parallel/ duplicate edges in a Graph in NetworkX

I am creating a Graph (can't use Multigraph) by iterating over a df. Some rows in the df are exactly the same. So, the edge won't be repeated in the graph. But, I'd like to have an attribute added to each edge with following possible values.

0 : edge does not have a duplicate in the df

1: edge has a duplicate in the df

For example, I have a df like this df

I'd like the edges (1,2), (1,3) to have an attribute duplicate=1and the edge (2,5) to have duplicate=0.

Upvotes: 0

Views: 1097

Answers (2)

Joel
Joel

Reputation: 23837

Use G.number_of_edges(1,2) to get the number of edges between 1 and 2 in a multigraph G. Here is the documentation.

Upvotes: 1

iacob
iacob

Reputation: 24251

Not knowing how you have coded this, in pseudo code you could do something like the following:

for row in df:
    if (row.from, row.to) in graph:
        graph[(row.from, row.to)].duplicate +=1
    else:
        graph.append[(row.from,row.to)]

Upvotes: 0

Related Questions