How to access specific elements of dictionary with function while iterating using Array.apply

User 2575 | 11/23/2015, 2:55:46 AM

Hi there, I'm using SArray.apply to transform a dictionary inside each row of a list on each call. I see the function defined in SArray.apply will be called for each row in the list but once inside the function being called I don't know how to indicate the number of iteration to access the dictionary corresponding to the row. In other words, is the number of the iteration somewhere where it can be used ? Is there a better way? Thanks


User 19 | 11/23/2015, 5:57:31 PM


One simple approach is to add a row number to the SFrame: sf = sf.add_row_number(). Now the row number will be available inside the lambda at row['id'].

Let us know if that helps! Chris

User 2575 | 11/23/2015, 10:23:26 PM

Thanks Chris. Let me be more specific with the code below. I'm trying to add a column to temp with the count of the word 'journal'. 'journal' could be in column 'wordcount' with holds a dictionary of word counts {'and':3, 'journal':4, ...} for each row of temp. If 'journal' is not in the dictionary I add a 0 to the corresponding row of temp['journal']. I'm using .apply to iterate through each row of temp. Each time I call the function journalcount and I check for the word 'journal' I don't know how to indicate the row number to access a different row each time journal_count in the "if" statement, therefore the test is always 'false'. Result should be a new temp with the column 'journal' added containing the counts for the word.

thanks again!

def journal_count(cont): if 'journal' in temp['word_count']: cont = temp['word_count']['journal'] return cont else: contador = 0 return cont temp['journal']=temp['wordcount'].apply(journalcount)