KeyError when assign colours to nodes

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)  

Answer

wwii answer solves one problem.

However there are a number of problems that need to be fixed:

  1. Only nodes in column Node will have color, users that are only introduced in Neighbors column will be created in G.add_edge(n,neighbor), and won’t have a color assigned. You need to decide which color to set for these nodes.

  2. The weight you want to attribute to the edges is being attributed to the nodes.

df = pd.DataFrame(  data = {"Node": ["Luke", "Luke", "Michael", "Ludo", "Alte", "Alte"],
                            "Neighbors": ["Ludo", "John", "Laura", "Stella", "Ludo", "Luke"],
                            "Colour": ["orange", "orange", "red", "orange", "blue", "blue"], 
                            "Weight": [3, 3 ,43, 21, 24, 24] 
                        }
              )
   

NROWS = None
def get_graph_from_pandas(df, v = False):
    
    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():
        print(row)
        n = row.Node
        w = row.Weight
        c = row.Colour
        neighbor = row.Neighbors
        
        G.add_node(n, weight = w, colour = c) # only nodes in column Node will have color
                                              # users that are only introduced in Neighbors column dwont have column
        if neighbor not in G.nodes:
            G.add_node(neighbor, weight = w, colour = "yellow") # this will set the default color to yellow
        G.add_edge(n,neighbor, weight = w) # weight of edge
            
    return G
        
G = get_graph_from_pandas(df, v = False)

print("Done.")
print("Total number of nodes: ", graph.number_of_nodes())
print("Total number of edges: ", graph.number_of_edges())

fig = plt.figure(figsize=(2,2))

pos = nx.draw(G, with_labels=True, 
              node_color=[node[1]['colour'] for node in G.nodes(data=True)], 
              node_size=200)

for node in G.nodes(data=True):
    try:
        node[1]['colour']
    except KeyError:
        print(node)