繁体   English   中英

python Pandas |如何将使用rake函数提取的关键字分配到新列中

[英]python Pandas |How to assign keywords extracted using rake function into a new column

我正在学习制作基于内容的图书推荐系统(参考: https : //towardsdatascience.com/how-to-build-from-scratch-a-content-based-movie-recommender-with-natural-language-processing-25ad400eb243 )。 我使用了rake函数从“图”列中提取关键字。 如何将这些关键字分配到新列?

我正在使用熊猫,numpy,CountVectorizer和rake_nltk。 我尝试了以下代码: row['Key_words'] = list(key_words_dict_scores.keys())但该列仍然为空。

import pandas as pd
from rake_nltk import Rake
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer

df = pd.read_csv('cleaned DATA set.csv')
df = df[['Book_ID','Title','Author','Genre1','Genre2','Plot']]


for index, row in df.iterrows():
    plot = row['Plot']

    # instantiating Rake, by default it uses english stopwords from NLTK
    # and discards all puntuation characters as well
    r = Rake()

    # extracting the words by passing the text
    r.extract_keywords_from_text(plot)

    # getting the dictionary whith key words as keys and their scores as values
    key_words_dict_scores = r.get_word_degrees()

    # assigning the key words to the new column for the corresponding movie
    row['Key_words'] = list(key_words_dict_scores.keys())

我希望看到添加了名为'Key_words'的新列,其中包含对应书名的所有关键字。

实际输出显示'key_words'空。

您错过了在for循环之前初始化新列的步骤。

df['Key_words'] = ""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM