2015-11-01 80 views
0

我在称为sf的SFrame中有一个叫做word_count的SArray。在word_count SArray中的每一行都包含一个字典。 我有一个名为selected_words 的数组我试图遍历每列以查看“selected_words”中哪些单词出现在列中。如果它看起来我拿到的价值,并写入一个新的列。 下面是只有一个字一个例子(“伟大”):使用apply()将值分配给新列

selected_words = ['awesome ', 'great'] 
def word_count(row): 
    if 'great' in row: 
      sf['great']=row['great'] 
    else: 
     abc="a" #nothing should happen 
sf['word_count'].apply(word_count) 

+-------------------------------+ 
|   word_count   | 
+-------------------------------+ 
| {'and': 5, '6': 1, 'stink'... | 
| {'and': 3, 'love': 1, 'it'... | 
| {'and': 2, 'quilt': 1, 'it... | 
| {'ingenious': 1, 'and': 3,... | 
| {'and': 2, 'parents!!': 1,... | 
| {'and': 2, 'this': 2, 'her... | 
| {'shop': 1, 'noble': 1, 'i... | 
| {'and': 2, 'all': 1, 'righ... | 
| {'and': 1, 'help': 1, 'giv... | 
| {'journal.': 1, 'nanny': 1... | 
+-------------------------------+ 


print sf['great'] 
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... ] 

据我已经明白,同样的值(1)被应用到每一行,但我只需要该行其中“伟大”一词实际上被发现。 我该怎么做?

回答

2

代码中的问题是,在每次调用函数word_count后,您都要更改整列sf ['great']。这里的另一种方法:

def word_count(d): 
    return d['great'] if 'great' in d else 0 

,之后将此功能顺丰[“WORD_COUNT”柱:

sf['great'] = sf['word_count'].apply(word_count) 
+0

如果我没有记错,这也是做一次手术的最快方法像这样一个DataFrame。 –