Edgelist from pandas dataframe with nodes of different colours

Solution 1:

Here's a way to do that:

df["color"] = "blue"
df.loc[df.Src.isin(["A", "D"]), "color"] = "green"

# The following line is needed because, at least in the way my dataset 
# is created, 'Dst' is not a list but rather a string. 
# For example, Dst of 'A' is the string "[A,B]". Here, 
# I'm converting it to the list ["A", "B"]
# If your data doesn't need this, just comment this line out. 
df["Dst"] = df.Dst.apply(lambda x: x[1:-1].split(","))

G = nx.from_pandas_edgelist(df.explode("Dst"), 'Src', 'Dst')
nx.draw(G, node_color = df.color)

The output is:

enter image description here