Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用apply()为新列赋值_Python_Graphlab - Fatal编程技术网

Python 使用apply()为新列赋值

Python 使用apply()为新列赋值,python,graphlab,Python,Graphlab,我在一个名为sf的SFrame中有一个名为word\u count的SArray。word\u countSArray中的每一行都包含一个dict。 我有一个名为selected\u words 我正在尝试循环浏览每一列,以查看“所选单词”中的哪些单词出现在该列中。如果出现,我将获取该值并将其写入新列。 下面是一个单词(“伟大”)的示例: 据我所知,相同的值(1)应用于每一行,但我只需要在实际找到单词“great”的那一行中使用它。 我如何才能做到这一点?代码中的问题是,在每次调用函数单词\u

我在一个名为sf的
SFrame
中有一个名为
word\u count
的SArray。
word\u count
SArray中的每一行都包含一个dict。 我有一个名为
selected\u words
我正在尝试循环浏览每一列,以查看“所选单词”中的哪些单词出现在该列中。如果出现,我将获取该值并将其写入新列。 下面是一个单词(“伟大”)的示例:

据我所知,相同的值(1)应用于每一行,但我只需要在实际找到单词“great”的那一行中使用它。
我如何才能做到这一点?

代码中的问题是,在每次调用函数单词\u count之后,您都要更改整列sf['great']。以下是另一种方法:

def word_count(d):
    return d['great'] if 'great' in d else 0
然后将此函数应用于sf['word_count']列:

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

如果我没有弄错的话,这也是使用数据帧执行类似操作的最快方法。
sf['great'] = sf['word_count'].apply(word_count)