books_over10['Keywords'] = ""
r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.
for index, row in books_over10.iterrows():
a=r.extract_keywords_from_text(row['bookTitle'])
c=r.get_ranked_phrases() # To get keyword phrases ranked with scores highest to lowest.
books_over10.at[index, 'Keywords'] = c
books_over10.head()
我正在使用上面的代码,以便处理所有行并从bookTitle列的每一行中提取关键字,然后将它们作为列表插入到同一行中名为关键字的新列中。问题是,是否存在一种更有效的方法来执行此操作而不需要遍历所有行,因为这会花费大量时间。任何帮助,将不胜感激。预先感谢!
昌明的解决方案:
def extractor(row):
a=r.extract_keywords_from_text(row)
return r.get_ranked_phrases() # To get keyword phrases ranked with scores highest to lowest.
r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.
books_over10['Keywords'] = books_over10['bookTitle'].map(lambda row : extractor(row))
答案 0 :(得分:2)
尝试查看map。不确定确切使用的是什么Rake,以及编码的方式有点令人困惑,但是一般的语法是。
books_over10['Keywords'] = books_over10['bookTitle'].map(lambda a: FUNCTION(a))