Appending to an array inside a column

User 984 | 7/7/2015, 2:44:02 AM

I have an SFrame with two columns where id is a scalar and scores is a vector, like so:s = gl.SFrame({"id":[1, 2, 3], "scores":[[4], [5], [6]]}). I can't seem to figure out how to append to one of the lists in the scores column.

For instance, say I want to append a 7, 8, and 9 to the scores corresponding to id 1. I imagined it would look something like this: s[s["id"] == 1]["scores"].append([7, 8, 9]). This approach seems to fail because I'm mixing SArrays and lists. What exactly is the syntax to achieve this? Essentially, I want to select the row with the id of interest so I can update the scores array in that row. Ideally, the correctly updated frame should look like: gl.SFrame({"id":[1, 2, 3], "scores":[[4, 7, 8, 9], [5], [6]]}).

Comments

User 1178 | 7/7/2015, 4:24:49 PM

Hi Bill,

You may combine join and apply operation. Here is an example:

`

test data

s1 = gl.SFrame({"id":[1, 2, 3], "scores":[[4], [5], [6]]}) s2 = gl.SFrame({"id":[2, 4, 6], "scores":[[2,3], [4,5], [6,7]]})

join to get two SFrames into one SFrame

tmp = s1.join(s2, on="id", how='outer')

combine two arrays into one

def combine(a1, a2): ret = None if a1 is None: ret = a2 elif a2 is None: ret = a1 else: a1.extend(a2) ret = a1 return ret

use apply function to combine the two scores

combined_scores = tmp.apply(lambda x: combine(x['scores'], x['scores.1']))

get the result

result = gl.SFrame({'id':tmp['id'], 'scores':combined_scores}) ` Let me know if this works for you.

Thanks! Ping


User 984 | 7/8/2015, 1:28:06 AM

Hi Ping,

So my issue was trying to update the SFrame column in place. What I needed to do was get the results after combining and use them as a column in a new SFrame.

Thanks for the help, Bill