Search code examples
pandasdataframegraph-tool

Graph-tool vertex label with add_edges_from


I tried my best to look for the solution online, but cannot seem to find one for my graph_tool network. I have a pandas dataframe df that has three columns: id_source, id_target, weight. The id_source and id_target contain names in text form. I want to use them as vertex labels. However, when adding edges from a pandas dataframe, I must set hashed=True. The code then looks like this

from graph_tool.all import *

g = Graph()
eweight = g.new_ep('double')
vmap = g.add_edge_list(df.values, eprops=[eweight], hashed=True)

My objective is to draw this small network with vertex labels. I am stuck and can't figure out the solution on how to add that vertex property when I do not know the order by which each new node is introduced in the graph through the g.add_edge_list() function.

Should I add the vertices first? And then perform the add_edge_list function on the graph. Will the vertex names (or labels) be recognized by the second step?


Solution

  • The answer is in the vmap. It's there all along. When you index the vmap, it will get the label names.

    • vmap[0] would be the label for the first vertex recorded.
    • vmap[1] for the second, etc.