Search code examples
graphlab

How to add 2 or more kinds of Vertices into SGraph in GraphLab Create?


I am using graphlab create in ubuntu. I try to add 2 kinds of vertices from 2 csv files using the following commands:

import graphlab as gl

v1 = gl.SFrame.read_csv('~/Documents/1.csv')
v2= gl.SFrame.read_csv('~/Documents/2.csv') 

g = g.add_vertices(vertices=v1, vid_field='name')
g = g.add_vertices(vertices=v2, vid_field='id')

But I found that it does not work. After I run the last command try to add the second kind of vertices, the vertices I added the first time got overwritten! How can I do it correctly? And how can I do it correctly to add 2 kinds of edges?

Thanks ahead!


Solution

  • In the following example, I create two sets of vertices and add them to a graph, then create two sets of edges and add them to the graph.

    >>> a = graphlab.SFrame({'id': [0, 1, 2, 3]})
    >>> b = graphlab.SFrame({'name': [5, 6, 7]})
    >>> g = graphlab.SGraph().add_vertices(a, 'id').add_vertices(b, 'name')
    >>> e1 = graphlab.SFrame({'id': [0, 0, 1], 'name': [6, 6, 5]})
    >>> e2 = graphlab.SFrame({'id': [2, 3], 'name': [5, 7]})
    >>> g = g.add_edges(e1, 'id', 'name').add_edges(e2, 'id', 'name')
    >>> g
    
    SGraph({'num_edges': 5, 'num_vertices': 7})
    Vertex Fields:['__id']
    Edge Fields:['__src_id', '__dst_id']