Reputation: 6270
I am trying to create out of a pandas dataframe a directed graph right now with networkx, so far i can use:
nx.from_pandas_edgelist(df, 'Activity', 'Activity followed', create_using=nx.DiGraph())
which shows me all the nodes and edges from Activity --> Activity followed.
In my dataframe there is sometimes the same activity followed by the same activity and i want to count this number in form of weights for the edges so far example this is my dataframe:
Index Activity Activityfollowed
0 Lunch Dinner
1 Lunch Dinner
2 Breakfast Lunch
should have the Edges:
Lunch --> Dinner (weight 2)
Breakfast --> Lunch (weight 1)
Is there any way to do it?
Upvotes: 3
Views: 3474
Reputation: 18647
You could try adding the weight
attribute as a column, using groupby.transform
, then pass the edge_attr
argument to the from_pandas_edgelist
method:
df['weight'] = df.groupby(['Activity', 'Activityfollowed'])['Activity'].transform('size')
G = nx.from_pandas_edgelist(df, 'Activity', 'Activityfollowed',
create_using=nx.DiGraph(), edge_attr='weight')
Confirm that it's worked using:
G.edges(data=True)
[out]
OutEdgeDataView([('Lunch', 'Dinner', {'weight': 2}), ('Breakfast', 'Lunch', {'weight': 1})])
Upvotes: 6