Use apply() to assign value to new column

核能气质少年 提交于 2019-12-12 06:02:29

问题


I have a SArray called word_count in a SFrame called sf. Every row in the word_count SArray consists of a dict. I have an array called selected_words I am trying to loop through every column to see which of the words from "selected_words" appears in the column. If it appears i take the value and write it into a new column. Here is an example for just one word ('great'):

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, ... ]

As far as I have understood, the same value(1) gets applied to every row, but i only need it in that row where the word 'great' was actually found. How can i do this?


回答1:


The problem in your code is that you are changing the full column sf['great'] after each call of the function word_count. Here's another approach :

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

and after that apply this function to the sf['word_count'] column :

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


来源:https://stackoverflow.com/questions/33461376/use-apply-to-assign-value-to-new-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!