Vertex attributes pandas

Here, mentions the use of a second Dataframe to add vertex attributes, but I cannot understand what it is saying.

From pandas DataFrame(s)

A common practice is to store edges in a pandas.DataFrame, where the two first columns are the source and target vertex ids, and any additional column indicates edge attributes. You can generate a graph via Graph.DataFrame() :

g = Graph.DataFrame(edges, directed=False)

It is possible to set vertex attributes at the same time via a separate DataFrame. The first column is assumed to contain all vertex ids (including any vertices without edges) and any additional columns are vertex attributes:

g = Graph.DataFrame(edges, directed=False, vertices=vertices)

Something like:

g = ig.Graph.DataFrame(df, vertices=df2)

nor does

g = ig.Graph.DataFrame(df, df2)

nor

g = ig.Graph.DataFrame(df, directed=False, vertices=df2)

Seems not work. I cannot colorize based on the attribute, and printing all

print(ig.vertex_attr(g))

finds Attribute does not exist

Tried some more:

os.chdir("C:/Users/bizo2/Desktop")

df = pd.read_excel("edgelist5.xlsx")
df2 = pd.read_excel("Book4.xlsx")
g = ig.Graph.DataFrame(df)
visual_style = {}
visual_style["vertex_size"] = 20

visual_style["vertex_label"] = g.vs["name"]

g.vs["RTD"] = df2['RTD'].values.tolist()

Book4 is literally just a column of values for attribute that aligns with the order of df, even when there are multiple vertices. From here

Tried some more and I think I found a legit bug. So the proper use seems to be:

g = ig.Graph.DataFrame(df,     vertices=df2)

Looking at the documentation, but I still get a value error

ValueError: Some vertices in the edge DataFrame are missing from vertices DataFrame

Which you would think is real, but checking in excel by first copying over from my edge list, removing duplicates(because there is more than one vertex to edge), and then comparing this to my vertices Book4 column, I have all of the vertices.

So at a loss

Ah so stupid me. There are vertices as a destination, which are not in the first column. Correcting this, now I am stuck at coloring. using:

import os
import igraph as ig
import pandas as pd


os.chdir("C:/Users/bizo2/Desktop")

df = pd.read_excel("edgelist4.xlsx")
df2 = pd.read_excel("Book4.xlsx")
g = ig.Graph.DataFrame(df, 	vertices=df2)
visual_style = {}
visual_style["vertex_size"] = 20

visual_style["vertex_label"] = g.vs["name"]

g.vs["RTD"] = df2['RTD'].values.tolist()

color_dict = {"TRUE": "blue", "FALSE": "pink"}

print(g.vertex_attributes())
g.vs["color"] = [color_dict[RTD] for RTD in g.vs["RTD"]]
layout=g.layout_fruchterman_reingold
visual_style["fr"] = layout
visual_style["bbox"] = (10000, 10000)

ig.plot(g, **visual_style)

I get

KeyError: False

for

---> 21 g.vs["color"] = [color_dict[RTD] for RTD in g.vs["RTD"]]

Which comes from here

g.vs[“color”] = [color_dict[gender] for gender in g.vs[“gender”]]

So I simply skipped overwriting my attribute based on the dictionary and forced color on my own. It worked.