pandas NetworkX From_Pandas_dataframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30041877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:18:41  来源:igfitidea点击:

NetworkX From_Pandas_dataframe

pythonpandasnetworkx

提问by user2723240

I have an error with NetworkX which says 'module' has no attribute 'from_pandas_dataframe.'

我有一个 NetworkX 错误,它说“模块”没有属性“from_pandas_dataframe”。

I have a dataframe called nflroster that is format as:

我有一个名为 nflroster 的数据框,其格式为:

Index   . . . Player           Team       Year

0       . . . Player1          Team1      2014

1       .  . .Player2          Team1      2014


2       . . . Player3          Team2      2014
.
.       . . .   .                .         .

So according to the documentation here networkx documentationthis following line should work

因此,根据此处的文档networkx 文档,以下行应该可以工作

G = nx.from_pandas_dataframe(nflroster,str, 'Team')

However when I run this in Ipy notebook I run into the error, 'module' object has no attribute 'from_pandas_dataframe'.

但是,当我在 Ipy notebook 中运行它时,我遇到了错误,“模块”对象没有属性“from_pandas_dataframe”。

I import the following

我导入以下内容

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

采纳答案by Gustavo Avila

I had the same problem here. This is how I solved it:

我在这里遇到了同样的问题。我是这样解决的:

Try installing networkx from source instead of installing it through pip.

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

Source Install Step by Step

一步一步的源安装

    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

Notice that this specific function From_Pandas_dataframewill be installed in convert_matrix.py file at the networkx folder.

请注意,此特定函数From_Pandas_dataframe将安装在 networkx 文件夹中的 convert_matrix.py 文件中。

回答by hlin117

You probably installed an incorrect version of networkx. You probably should check whether you have 1.10.0 <= see history here

您可能安装了错误版本的 networkx。您可能应该检查您是否有 1.10.0 <=在此处查看历史记录

回答by marianstefi20

If we look in the networkx's build folder, in __init__.py, we see an import from networkx.convert_matrix. Looking thru the convert_matrix.pyfile we can see the following allowed external dependencies:

如果我们查看 networkx 的构建文件夹,在__init__.py 中,我们会看到来自 networkx.convert_matrix 的导入。查看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']

As you can see, from_pandas_dataframe does not exist. Although this might have been in the 1.10 version.

如您所见, from_pandas_dataframe 不存在。虽然这可能是在1.10 版本中

So in the end, always keep a keen eye on the version number.

所以最后,请始终密切关注版本号。

回答by MaxU

yet another way to search for a method in Networkx API:

在 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']