0

这是我的代码:

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 给了我KeyError: 'Abrev'这个代码的一个关键错误,但我也尝试更改'Abrev'0删除source='Abrev'所有代码,但每个错误都会略有不同;'KeyError: 0 'KeyError: 'source'

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 |
+---+---+---+---+---+
4

1 回答 1

0

只需添加,而您在前面nx.from_pandas_adjacency忘记了nx.enumerate_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))))
于 2021-03-25T21:57:33.700 回答