Python 来自\u熊猫\u数据帧的NetworkX

Python 来自\u熊猫\u数据帧的NetworkX,python,pandas,networkx,Python,Pandas,Networkx,NetworkX有一个错误,它说“模块”没有“来自数据帧”的属性 我有一个名为nflroster的数据帧,其格式如下: Index . . . Player Team Year 0 . . . Player1 Team1 2014 1 . . .Player2 Team1 2014 2 . . . Player3 Team2 201

NetworkX有一个错误,它说“模块”没有“来自数据帧”的属性

我有一个名为nflroster的数据帧,其格式如下:

Index   . . . Player           Team       Year

0       . . . Player1          Team1      2014

1       .  . .Player2          Team1      2014


2       . . . Player3          Team2      2014
.
.       . . .   .                .         .
因此,根据这里的文档,下面这一行应该可以工作

G = nx.from_pandas_dataframe(nflroster,str, 'Team')
然而,当我在Ipy笔记本中运行这个时,我遇到了错误,“module”对象没有“from_pandas_dataframe”属性

我导入以下内容

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame as df

我也有同样的问题。我就是这样解决的:

尝试从源代码安装networkx,而不是通过pip安装

源代码逐步安装

    Download the source (tar.gz or zip file) from https://pypi.python.org/pypi/networkx/ or get the latest development version from https://github.com/networkx/networkx/
    Unpack and change directory to the source directory (it should have the files README.txt and setup.py).
    Run python setup.py install to build and install

请注意,来自数据帧的此特定函数将安装在networkx文件夹的convert\u matrix.py文件中。

您可能安装了不正确的networkx版本。如果我们查看networkx的build文件夹中的\uuu init\uuuuuuuuuuuuuuupy,我们会看到从networkx.convert\u矩阵导入的内容,那么您可能应该检查是否有1.10.0。通过查看convert_matrix.py文件,我们可以看到以下允许的外部依赖项:

__all__ = ['from_numpy_matrix', 'to_numpy_matrix',
           'from_pandas_adjacency', 'to_pandas_adjacency',
           'from_pandas_edgelist', 'to_pandas_edgelist',
           'to_numpy_recarray',
           'from_scipy_sparse_matrix', 'to_scipy_sparse_matrix',
           'from_numpy_array', 'to_numpy_array']
如您所见,from_pandas_数据帧不存在。虽然这可能是在未来的日子里


因此,最后,请始终密切关注版本号。

在Networkx API中搜索方法的另一种方法:

In [199]: [m for m in nx.__dir__() if 'pandas' in m]
Out[199]:
['from_pandas_adjacency',
 'to_pandas_adjacency',
 'from_pandas_edgelist',
 'to_pandas_edgelist']

你检查过你的版本了吗?该函数似乎是在2.0版中添加的,您可能正在使用旧版本。您必须从源代码而不是pip下载。我不知道为什么,但我一直和皮普有问题。u/Gustavo Avila/有正确的解决方案。我遇到了相同的问题,从源代码安装后仍然返回错误。