【问题标题】:python Pandas |How to assign keywords extracted using rake function into a new columnpython Pandas |如何将使用rake函数提取的关键字分配到新列中
【发布时间】:2019-12-01 14:37:29
【问题描述】:

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

我正在使用 pandas、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' 列是空的。

【问题讨论】:

标签: python pandas jupyter-notebook cosine-similarity countvectorizer


【解决方案1】:

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

df['Key_words'] = ""

【讨论】:

  • 哦,我很抱歉。我确实做了这一步,但我忘了在这篇文章中添加它。即使初始化新列也不会产生我需要的结果。
  • 你能告诉我我还缺少什么吗?我对 python 和 NLP 很陌生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多