I have a dataframe as below.
import pandas as pd
import networkx as nx
df = pd.DataFrame({'source': ('a','a','a', 'b', 'c', 'd'),'target': ('b','b','c', 'a', 'd', 'a'), 'weight': (1,2,3,4,5,6) })
I want to convert it to directed networkx multigraph. I do
G=nx.from_pandas_dataframe(df, 'source', 'target', ['weight'])
& get
G.edges(data = True)
[('d', 'a', {'weight': 6}),
('d', 'c', {'weight': 5}),
('c', 'a', {'weight': 3}),
('a', 'b', {'weight': 4})]
G.is_directed(), G.is_multigraph()
(False, False)
But i want to get
[('d', 'a', {'weight': 6}),
('c', 'd', {'weight': 5}),
('a', 'c', {'weight': 3}),
('b', 'a', {'weight': 4}),
('a', 'b', {'weight': 2}),
('a', 'b', {'weight': 4})]
I have found no parameter for directed & multigraph in this manual.
I can save df as txt and use nx.read_edgelist() but it's not convinient
G=nx.from_pandas_edgelist(df, 'source', 'target', ['weight'])works correctlycreate_usingargument which takes different graph types. I haven't tried this personally, but perhaps some luck with that?