使用apply()为新列分配值 [英] Use apply() to assign value to new column

查看:86
本文介绍了使用apply()为新列分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为sf的SFrame中有一个名为word_count的SArray. word_count SArray中的每一行都包含一个字典. 我有一个名为selected_words的数组 我试图遍历每一列,以查看"selected_words"中的哪些单词出现在该列中.如果出现,我将值取值并将其写入新列. 这是一个单词("great")的示例:

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

据我所知,相同的value(1)应用于每行,但是我只需要在实际上找到单词"great"的那一行中使用它. 我该怎么办?

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?

推荐答案

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

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

之后,将此功能应用于sf ['word_count']列:

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

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

这篇关于使用apply()为新列分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆