Reputation: 557
I have a dataset with two columns each represents a participant (name, surname) of accident. So I made a graph from this data, and now I need to count the number of edges for each participant i.e. how many times a person was in accident with others.
I use networkx
lib for this, it has .number_of_edges()
method but it counts 1 for every participant.
df = pd.read_excel('выборка.xlsx', index_col='№ страхового события')
df.columns = ['part_1', 'part_2']
G=nx.from_pandas_dataframe(df, 'part_1', 'part_2')
for i in G:
for j in G:
if G.number_of_edges(j, i) > 0:
print(G.number_of_edges(j, i))
What am I doing wrong?
Upvotes: 0
Views: 2086
Reputation: 7293
Your code is not doing what you think it does. You are counting the number of times a participant (i) is in an accident with a specific other one (j), which is likely to be 1.
If you want for each participant, the number of times it has an accident with any other participant, you can use for instance
for i in G:
print(len(G.edges(i)))
Upvotes: 2