简体   繁体   English

KeyError:from_pandas_edge_list() 的“来源”

[英]KeyError: 'source' for from_pandas_edge_list()

Here is my code:这是我的代码:

import numpy as np
import pandas as pd
import networkx as nx

df = pd.read_excel(r"/path/to/file.xlsx", sheet_name="Sheet4")
df.edge=nx.from_pandas_edgelist(df,source='Abrev')
print(list(enumerate_all_cliques(nx.Graph(df.edge))))

if gives me a key error of KeyError: 'Abrev' for this code but I also tried changing 'Abrev' to 0 and deleting the source='Abrev' all together and just get a slight different error for each; if 给我一个KeyError: 'Abrev'的关键错误,但我也尝试将'Abrev'更改为0并一起删除source='Abrev' ,每个错误都会略有不同; 'KeyError: 0 'KeyError: 'source' 'KeyError: 0 'KeyError: 'source'

Sample contents of excel file; excel文件示例内容;

+---+---+---+---+---+
|   | A | B | C | D |
+---+---+---+---+---+
| A | 0 | 1 | 1 | 0 |
+---+---+---+---+---+
| B | 1 | 0 | 0 | 1 |
+---+---+---+---+---+
| C | 1 | 0 | 0 | 1 |
+---+---+---+---+---+
| D | 0 | 1 | 1 | 0 |
+---+---+---+---+---+

just add the nx.from_pandas_adjacency and you forgot a nx.只需添加nx.from_pandas_adjacency ,你就忘记了nx. in front of the enumerate_all_cliquesenumerate_all_cliques前面

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

df = pd.read_excel(r"/path/to/file.xlsx", sheet_name="Sheet4",index_col=0,usecols = "A:E")
df.edge=nx.from_pandas_adjacency(df)
print(list(nx.enumerate_all_cliques(nx.Graph(df.edge))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM