KeyError: 'source' for from_pandas_edge_list()

1.1k views Asked by At

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; 'KeyError: 0 'KeyError: 'source'

Sample contents of excel file;

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

There are 1 answers

0
mewspoon On

just add the nx.from_pandas_adjacencyand you forgot a nx. in front of the 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))))