模块 'networkx' 没有属性 'from_pandas_edgelist'

module 'networkx' has no attribute 'from_pandas_edgelist'

这是我的代码:

employee_movie_choices = pd.read_csv('Employee_Movie_Choices.txt', sep="\t")
B = nx.from_pandas_edgelist(employee_movie_choices, '#Employee', 'Movie')

并且出现错误:AttributeError: module 'networkx' has no attribute 'from_pandas_edgelist'* 但是,我们可以在networx 的文档中找到networkx 具有该属性。 这是文件的 link:from_pandas_edgelist

为什么会出现这个问题?

您是否按如下方式定义别名 nx:

import networkx as nx

如果是,尝试调用所需的函数如下:

import networkx as nx 
......
......
nx.convert_matrix.from_pandas_edgelist(...)

也许你可以使用另一个函数from_pandas_dataframe

import pandas as pd
import networkx as nx
edges = pd.DataFrame()
# start
edges['sources'] = [1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5]
# end
edges['targets'] = [2, 4, 5, 3, 1, 2, 5, 1, 5, 1, 3, 4]
# weight
edges['weights'] = [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1]

# build the graph
G = nx.from_pandas_dataframe(
    edges,
    source='sources',
    target='targets',
    edge_attr='weights')