1

I have a dataframe as follows and I work a directed Graph.

In this case, You can find that there are same interactions between '1' and '3' in the first and last lines.

I find that nx.degree() function is only applied the last event of same nodes' interactions.

I would like to get degree() considering all the event between same nodes in the networkx syntax.

  sender receiver  amt
0      1        3   50
3      2        1    1
4      1        3  100
test = pd.DataFrame({'sender' : ['1','2','1'], 
                   'receiver' : ['3','1','3'], 
                   'amt' : [50,1,100]})

H = nx.from_pandas_edgelist(test, source = 'sender', target = 'receiver',
                            create_using=nx.DiGraph(), edge_attr = 'amt')

H.out_degree(weight = 'amt')
# this is a result :  {'1': 100, '3': 0, '2': 1}
# However I want to get this result : {'1': 150, '3': 0, '2': 1}
1
  • If there is no other way, I think I have to process the data with pandas and groupby.sum(). Commented Jul 26, 2020 at 6:03

1 Answer 1

1

You're getting that out degree because only the last edge is considered. You cannot have multiple edges connecting to the same ends in a directed graph. Considering the output you expect, what you can do is groupby and sum those weights beforehand:

G = nx.from_pandas_edgelist(test.groupby(['sender','receiver'], as_index=False).amt.sum(), 
                            source = 'sender', 
                            target = 'receiver',
                            create_using=nx.DiGraph(), 
                            edge_attr = 'amt')

G.out_degree(weight = 'amt')
# OutDegreeView({1: 150, 3: 0, 2: 1})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.