Dino C
Dino C

Reputation: 317

Building MultiGraph from pandas dataframe - "TypeError: unhashable type: 'dict'"

I am experiencing the same issue, as it is described here

Networkx Multigraph from_pandas_dataframe

Although I replaced line 211 in convert_matrix.py, "TypeError: unhashable type: 'dict'" still exists. I want to build a MultiGraph using the following dataframe (links):

   1_id   f   v      v_id_1   v_id_2
0  3483  50  38000     739    2232
1  3482  50  38000     717    2196
2  3482  50  22000     717    2196
3  3480  50  22000    1058    2250

data = {'1_id':[3483, 3482, 3482, 3480], 'v_id_1':[739, 717, 717, 1058], 'v_id_2':[2232,2196, 2196, 2250], 'v':[38000, 38000, 22000, 22000], 'f':[50, 50, 50, 50]}
links = pd.DataFrame(data)

G=nx.from_pandas_dataframe(links, 'v_id_1', 'v_id_2', edge_attr=['v','f'], create_using=nx.MultiGraph())

Trying to create the MultiGraph I'm getting the error:

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'

Upvotes: 2

Views: 792

Answers (1)

Dino C
Dino C

Reputation: 317

After posting this issue in GitHub (see here link), I got a good answer, which at least in my case seems to work. I had installed networkx 1.11 insted of version 2.0.dev_20161206165920 Try to install the development version of NetworkX from github link

Upvotes: 2

Related Questions