Networkx Multigraph from_pandas_dataframe
That's a nice question. I tried to reproduce your problem building your MultiGraph()
in a different way, using only three/four columns with:
MG = nx.MultiGraph()
MG.add_weighted_edges_from([tuple(d) for d in df[['gene1','gene2','conf']].values])
this correctly returns as MG.edges(data=True)
:
[('geneA', 'geneB', {'weight': 0.05}), ('geneA', 'geneB', {'weight': 0.45}), ('geneA', 'geneC', {'weight': 0.45}), ('geneA', 'geneD', {'weight': 0.35})]
I tried also with your from_pandas_dataframe
method using only three columns but it doesn't work:
MG = nx.from_pandas_dataframe(df, 'gene1', 'gene2', edge_attr='conf', create_using=nx.MultiGraph())
this returns the same error you encountered. I don't know if it is a bug or that method doesn't support more than one weight type for MultiGraph()
. In the meantime you can use the above workaround to build your MultiGraph, at least with only one weight type. Hope that helps.
Networkx < 2.0:
It's was a bug, I opened an issue on GitHub, once I made the suggested edit:
It changed line 211 of convert_matrix.py
to to read:
g.add_edge(row[src_i], row[tar_i], attr_dict={i:row[j] for i, j in edge_i})
Results from that change: (which have since been incorporated)
MG= nx.from_pandas_dataframe(df, 'gene1', 'gene2', edge_attr=['conf','type'],
create_using=nx.MultiGraph())
MG.edges(data=True)
[('geneA', 'geneB', {'conf': 0.05, 'type': 'method1'}),
('geneA', 'geneB', {'conf': 0.45, 'type': 'method2'}),
('geneA', 'geneC', {'conf': 0.45, 'type': 'method1'}),
('geneA', 'geneD', {'conf': 0.35, 'type': 'method1'})]
Networkx >= 2.0:
In DataFrames with this format (edge list), use from_pandas_edgelist
MG= nx.from_pandas_edgelist(df, 'gene1', 'gene2', edge_attr=['conf','type'],
create_using=nx.MultiGraph())
MG.edges(data=True)
MultiEdgeDataView([('geneA', 'geneB', {'conf': 0.05, 'type': 'method1'}),
('geneA', 'geneB', {'conf': 0.45, 'type': 'method2'}),
('geneA', 'geneC', {'conf': 0.45, 'type': 'method1'}),
('geneA', 'geneD', {'conf': 0.35, 'type': 'method1'})])