2015-11-01 76 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。 –