簡體   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