pandas AttributeError: 模块“networkx”没有属性“from_pandas_dataframe”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49223057/
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-08 15:49:49  来源:igfitidea点击:

AttributeError: module 'networkx' has no attribute 'from_pandas_dataframe'

pandasnetworkx

提问by Krishna Neupane

I have networkx v. 2.1. to make it work w/ pandas dataframe, i tried following:

我有networkx v. 2.1。为了使其与 Pandas 数据框一起工作,我尝试了以下操作:

  • installed via pip3, this did not work generated Atrribute Erroras in title, hence uninstalled.
  • re-installed with 'python3 setup.py install"
  • 通过 安装pip3,这Atrribute Error在标题中不起作用,因此已卸载。
  • 用' python3 setup.py install"重新安装

Error description.

错误描述。

AttributeError: module 'networkx' has no attribute 'from_pandas_dataframe`

AttributeError: 模块“networkx”没有属性“from_pandas_dataframe”

Steps to reproduce Error:

重现错误的步骤:

I imported data using csv. I did this because I just wanted to read only 5000 rows from the dataset.

我使用csv. 我这样做是因为我只想从数据集中读取 5000 行。

x=pd.DataFrame([x for x in rawData[:5000]])

x[:10] 

0   1   2
0   228055  231908  1
1   228056  228899  1
2   228050  230029  1
3   228059  230564  1
4   228059  230548  1
5   70175   70227   1
6   89370   236886  1
7   89371   247658  1
8   89371   249558  1
9   89371   175997  1

g_data=G=nx.from_pandas_dataframe(x)

module 'networkx' has no attribute 'from_pandas_dataframe'

I know I am missing the from_pandas_dataframebut cant find a way to install it.

我知道我错过了from_pandas_dataframe但找不到安装它的方法。

[m for m in nx.__dir__() if 'pandas' in m] 

['from_pandas_adjacency',
 'to_pandas_adjacency',
 'from_pandas_edgelist',
 'to_pandas_edgelist']

回答by tohv

In networkx 2.0 from_pandas_dataframehas been removed.

在 networkx 2.0 中from_pandas_dataframe已被删除

Instead you can use from_pandas_edgelist.

相反,您可以使用from_pandas_edgelist.

Then you'll have:

那么你将拥有:

g_data=G=nx.from_pandas_edgelist(x, 1, 2, edge_attr=True)

回答by William Pourmajidi

A simple graph:

一个简单的图形:

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

# Build a dataframe with 4 connections
df = pd.DataFrame({'from': \['A', 'B', 'C', 'A'\], 'to': \['D', 'A', 'E', 'C'\]})

# Build your graph
G = nx.from_pandas_edgelist(df, 'from', 'to')

# Plot it
nx.draw(G, with_labels=True)
plt.show()

enter image description here

在此处输入图片说明

回答by Krishna Neupane

This solutionhelps as well:

解决方案也有帮助:

G = nx.DiGraph()
G.add_weighted_edges_from([tuple(x) for x in x.values])
nx.info(G)


'Name: \nType: DiGraph\nNumber of nodes: 5713\nNumber of edges: 5000\nAverage in degree:   0.8752\nAverage out degree:   0.8752'