use*_*240 5 python networkx pandas
我有一个 NetworkX 错误,它说“模块”没有属性“from_pandas_dataframe”。
我有一个名为 nflroster 的数据框,其格式为:
Index . . . Player Team Year
0 . . . Player1 Team1 2014
1 . . .Player2 Team1 2014
2 . . . Player3 Team2 2014
.
. . . . . . .
Run Code Online (Sandbox Code Playgroud)
因此,根据此处的文档networkx 文档,以下行应该可以工作
G = nx.from_pandas_dataframe(nflroster,str, 'Team')
Run Code Online (Sandbox Code Playgroud)
但是,当我在 Ipy notebook 中运行它时,我遇到了错误,“模块”对象没有属性“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
Run Code Online (Sandbox Code Playgroud)
我在这里也遇到了同样的问题。这就是我解决它的方法:
尝试从源代码安装 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
Run Code Online (Sandbox Code Playgroud)
请注意,此特定函数From_Pandas_dataframe将安装在 networkx 文件夹中的 Convert_matrix.py 文件中。