2016-02-04 68 views
5

嘗試使用networkx的from_pandas_dataframe從pandas DataFrame創建MultiGraph()實例。我在下面的例子中做錯了什麼?Networkx Multigraph from_pandas_dataframe

In [1]: import pandas as pd 
     import networkx as nx 

     df = pd.DataFrame([['geneA', 'geneB', 0.05, 'method1'], 
          ['geneA', 'geneC', 0.45, 'method1'], 
          ['geneA', 'geneD', 0.35, 'method1'], 
          ['geneA', 'geneB', 0.45, 'method2']], 
          columns = ['gene1','gene2','conf','type']) 

第一次嘗試用默認的nx.Graph():

In [2]: G= nx.from_pandas_dataframe(df, 'gene1', 'gene2', edge_attr=['conf','type'], 
            create_using=nx.Graph()) 

作爲非多重圖(),我錯過了重複邊緣之一:

In [3]: G.edges(data=True) 
Out[3]: [('geneA', 'geneB', {'conf': 0.45, 'type': 'method2'}), 
     ('geneA', 'geneC', {'conf': 0.45, 'type': 'method1'}), 
     ('geneA', 'geneD', {'conf': 0.35, 'type': 'method1'})] 

With MultiGraph()

In [4]: MG= nx.from_pandas_dataframe(df, 'gene1', 'gene2', edge_attr=['conf','type'], 
          create_using=nx.MultiGraph()) 

此:

TypeError         Traceback (most recent call last) 
<ipython-input-49-d2c7b8312ea7> in <module>() 
----> 1 MG= nx.from_pandas_dataframe(df, 'gene1', 'gene2', ['conf','type'], create_using=nx.MultiGraph()) 

/usr/lib/python2.7/site-packages/networkx-1.10-py2.7.egg/networkx/convert_matrix.pyc in from_pandas_dataframe(df, source, target, edge_attr, create_using) 
    209   # Iteration on values returns the rows as Numpy arrays 
    210   for row in df.values: 
--> 211    g.add_edge(row[src_i], row[tar_i], {i:row[j] for i, j in edge_i}) 
    212 
    213  # If no column names are given, then just return the edges. 

/usr/lib/python2.7/site-packages/networkx-1.10-py2.7.egg/networkx/classes/multigraph.pyc in add_edge(self, u, v, key, attr_dict, **attr) 
    340    datadict.update(attr_dict) 
    341    keydict = self.edge_key_dict_factory() 
--> 342    keydict[key] = datadict 
    343    self.adj[u][v] = keydict 
    344    self.adj[v][u] = keydict 

TypeError: unhashable type: 'dict' 

問題 如何實例化從熊貓數據幀一MultiGraph()

回答

6

這是一個錯誤,在GitHub上開了一個問題,一旦我作出的建議edit

改變的線211 convert_matrix.py來讀:從變化

g.add_edge(row[src_i], row[tar_i], attr_dict={i:row[j] for i, j in edge_i}) 

結果:

MG= nx.from_pandas_dataframe(df, 'gene1', 'gene2', edge_attr=['conf','type'], 
           create_using=nx.MultiGraph()) 

In [5]: MG.edges(data=True) 
Out[5]: [('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'})] 
+1

它對'MultiDiGraph'完全相同 – LancelotHolmes

4

這是一個很好的問題。我試圖重現您的問題構建MultiGraph()以不同的方式,只用三/四列有:

MG = nx.MultiGraph() 

MG.add_weighted_edges_from([tuple(d) for d in df[['gene1','gene2','conf']].values]) 

這個正確返回爲MG.edges(data=True)

[('geneA', 'geneB', {'weight': 0.05}), ('geneA', 'geneB', {'weight': 0.45}), ('geneA', 'geneC', {'weight': 0.45}), ('geneA', 'geneD', {'weight': 0.35})] 

我使用也試圖與您from_pandas_dataframe方法只有三列,但它不起作用:

MG = nx.from_pandas_dataframe(df, 'gene1', 'gene2', edge_attr='conf', create_using=nx.MultiGraph()) 

這將返回您遇到的相同錯誤。我不知道它是否是一個錯誤,或者該方法不支持MultiGraph()的多個重量類型。在此期間,您可以使用上述解決方法來構建MultiGraph,至少只能使用一種重量類型。希望有所幫助。

+0

我現在將使用解決方法,謝謝,感興趣的看看是否有人看到類似的錯誤。 – Kevin

+0

@凱文... 2年後,我得到了同樣的錯誤。在我的例子中,我試圖創建一個'MultiDiGraph()',但是和你一樣得到了同樣的錯誤。這個答案也解決了我的問題 – Sosi