NetworkX从Pandas数据框

2024-10-01 17:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在NetworkX中有一个错误,它说“module”没有属性“from_pandas_dataframe”

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

Index   . . . Player           Team       Year

0       . . . Player1          Team1      2014

1       .  . .Player2          Team1      2014


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

因此根据这里的文档networkx documentation这一行应该可以工作

^{pr2}$

但是,当我在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

Tags: 数据fromimportnetworkxdataframepandasindex属性
3条回答

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

尝试从源代码安装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

请注意,来自_Pandas_dataframe的特定函数将安装在convert中_矩阵.pynetworkx文件夹中的文件。在

如果我们查看networkx的build文件夹,在\uuinit_uj.py中,我们会看到一个从networkx.convert_矩阵. 通过转换_矩阵.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_dataframe不存在。虽然这可能在1.10 version中。在

所以最后,一定要密切关注版本号。在

您可能安装了不正确的networkx版本。您可能应该检查是否有1.10.0<;=seehistory here

相关问题 更多 >

    热门问题