python - Networkx Multigraph from_pandas_dataframe

标签 python pandas networkx

更新:
所写的问题与 Networkx 版本 < 2.0 相关。 from_pandas_dataframe 方法 has been dropped .
要在 Networkx >= 2.0 中完成相同的任务,请参阅已接受答案的更新。

尝试创建 MultiGraph()使用 networkx 的 from_pandas_dataframe 来自 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())

作为非 MultiGraph(),我遗漏了一条重复边:

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'})]

使用 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'

问题 如何从 pandas 数据帧实例化 MultiGraph()

最佳答案

Networkx < 2.0:
这是一个错误,我在 GitHub 上打开了一个问题,一旦我提出了建议 edit :

它将 convert_matrix.py 的第 211 行更改为:

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())

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:
在具有这种格式(边缘列表)的 DataFrame 中,使用 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'})])

关于python - Networkx Multigraph from_pandas_dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35210724/

相关文章:

python - 具有滞后值(value)的条件产品的 Pandas cumsum?

python-2.7 - "Fontconfig error: Cannot load default config file"在 Ubuntu 16.04 上

python - 如何在 Django 中动态更改数据库?

python - 优化python代码——过滤numpy数组

python - 检查列中的值是否唯一,如果它们是唯一的,则添加到行尾如果不唯一,则添加不唯一到行尾

python - 计算每行 DataFrame 中第一个有效值和最后一个有效值之间的差异?

python - 使用networkx快速生成大型无标度图

python - 在 networkx 中标记边

python - 如何从Python数组中获取匹配的记录?

python - Pandas 在多个大型数据帧中找到共同的 NA 记录