I am getting a KeyError when I try to create a network.
My dataset is
Node Neighbors Colour Weight
Luke Alte orange 3
Luke John orange 3
Michael Laura red 43
Ludo Stella orange 21
Alte Ludo blue 24
Alte Luke blue 24
The table above shows the links by nodes:
- node Luke is linked with Alte and John. It has edge weight 3 and colour orange
- node Michael is linked with Laura. It has weight 43 and colour red
- node Ludo is linked with Stella. It has weight 21 and colour orange
- node Alte is linked with Luke and Ludo. It has colour blue and weight 24
Doing as follows:
NROWS = None
def get_graph_from_pandas(df):
G = nx.DiGraph() # assuming the graph is directed since e.g node 1 has
# 3 as neighbour but 3 doesnt have 1 as neighbour
for row in df.itertuples(): # row is the row of the dataframe
n = row.Node
w = row.Weight
c = row.Colour
neighbors = row.Neighbors
G.add_node(n, weight = w, colour = c)
for neigh in neighbors:
#add edge weights here, attribute of G.add_edge
G.add_edge(n,neigh)
return G
G = get_graph_from_pandas(df)
print("Done.")
print("Total number of nodes: ", G.number_of_nodes())
print("Total number of edges: ", G.number_of_edges())
pos = nx.draw(G, with_labels=True,
node_color=[node[1]['colour'] for node in G.nodes(data=True)],
node_size=200)
gives me a KeyError: 'colour'.
When I print
for node in G.nodes(data=True):
try:
node[1]['colour']
except KeyError:
print(node)
I get
('A', {})
('l', {})
('t', {})
('e', {})
Can you please explain what is causing the error? Thanks
Update: I think the error is from here
for neigh in neighbors:
#add edge weights here, attribute of G.add_edge
G.add_edge(n,neigh)