Adding new fields in SGraph "vertices" or "edges" tables through `join`

User 1129 | 1/27/2015, 1:33:21 PM

We can add a new field to either "vertices" or "edges" table of an SGraph like this <code class="CodeInline">thegraph.vertices['foo'] = somevalue</code>. I would like to do this by using <code>join</code>, so that I can combine the information stored in the graph with external information, without the need to store an additional table.


User 18 | 1/28/2015, 12:38:18 AM

You can probably do a left join and assign the result to the vertices SFrame. Something like this:

<pre> thegraph.vertices = thegraph.vertices.join(newinfo, on={'name': 'nameinnewinfo'}, how='left') </pre>

User 1129 | 1/28/2015, 9:33:19 AM

This was my intuition too, but on my computer I get <code class="CodeInline">AttributeError: can't set attribute</code>.

What version of GraphlabCreate do you use?

User 398 | 1/28/2015, 6:26:03 PM

SGraphs are structurally immutable, but data mutable. This constraint explains why you can't reassign to the_graph.vertices. There are few different ways you can do what you're trying to do, but I think this is the simplest:

<pre class="CodeBlock"><code>newvertices = thegraph.vertices.join(newinfo, on={'name': 'nameinnewinfo'}, how='left') thegraph.addvertices(new_vertices)</code></pre>

User 18 | 1/28/2015, 6:38:19 PM

Ah, I think I see the problem with my original solution now. SGraph doesn't allow you to assign a whole new SFrame as the vertex table, but SFrames allow for addition or deletion of columns (SArrays). So the following should work, as well as Robert's solution.

<pre class="CodeBlock"><code>newvertexproperties = thegraph.vertices.join(newinfo, on={'name': 'nameinnewinfo'}, how='left') thegraph.vertices.addcolumns(newvertex_properties)</code></pre>

User 1129 | 1/28/2015, 6:57:31 PM

Alice: thanks. This is an elegant solution.

User 18 | 1/28/2015, 7:08:34 PM

Robert's solution might be cleaner since it'll avoid adding a duplicate __id column. This is just an alternate solution.

User 1129 | 1/29/2015, 8:28:08 AM

Now I get it: because of the function name, I was assuming that add_vertices actually adds new vertices.

User 398 | 1/29/2015, 5:50:44 PM

That wasn't a bad assumption. It can in fact be used to add new vertices, but it will also potentially overwrite existing vertices based on __id.