簡體   English   中英

Python - 將來自 Vader 的正面/負面/中立/反饋情緒評分拆分為單獨的列並將其添加到數據集中

[英]Python - Splitting positive/negative/neutral/ feedback sentiment score from Vader into separate columns and adding it to data set

我正在嘗試使用 python 中的 VADER 確定客戶反饋的情緒得分。 下面的簡單代碼非常適合個人反饋,並返回一個包含負面、中性、正面和復合分數的字典。

代碼:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer


feedback = "Food was very good"
vader = SentimentIntensityAnalyzer()
sentiment = vader.polarity_scores(feedback)

print(sentiment)

結果:{'neg':0.0,'neu':0.484,'pos':0.516,'compound':0.4927}

現在,我有一個包含 4k+ 客戶反饋的電子表格。 我要做的是遍歷每個反饋並添加 4 個新列作為 Negative_Score、Neutral_Score、Positive_Score 和 Compound_Score。 我編寫了以下代碼,但沒有給出預期的結果。 每行獲得相同的分數。 任何幫助將不勝感激。

代碼:

    import os.path
    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
    import pandas as pd

    data = pd.read_excel(r"C:\...\sample_feedback.xlsx") 
    #Sample_feedback.xlsx has two col customer and feedbacktext
    vader = SentimentIntensityAnalyzer()
    data["Negative_Score"] = vader.polarity_scores(data["feedbacktext"]).get("neg")
    data

我們可以使用lambda來實現這一點。 它比遍歷數據框行要好。 我寫了一個方法vader_scores唯一的 function 是從反饋文本中返回相應的極性分數(pos/neg/neu/compound)。

您的代碼為所有行返回相同極性分數的主要原因是您單獨使用了data["feedbacktext"] ,它占用了整個列並為其返回了結果值。 我所做的是,使用行的索引從每一行中選擇數據[“feedbacktext”],並在Negative_Score, Neutral_Score, Positive_Score, and Compound_Score列中填充了其單元格。

我使用的樣本數據是:

在此處輸入圖像描述

這是我的代碼:

import os.path
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd

def vader_scores(feedbacktext, category):
    return vader.polarity_scores(feedbacktext).get(category)

data = pd.read_excel(r"sample_feedback.xls") 
# print(data)
#Sample_feedback.xlsx has two col customer and feedbacktext
vader = SentimentIntensityAnalyzer()

data["Negative_Score"] = data.apply(lambda row : vader_scores(data["feedbacktext"][row.name], "neg"),axis=1)
data["Neutral_Score"] = data.apply(lambda row : vader_scores(data["feedbacktext"][row.name], "neu"),axis=1)
data["Positive_Score"] = data.apply(lambda row : vader_scores(data["feedbacktext"][row.name], "pos"),axis=1)
data["Compound_Score"] = data.apply(lambda row : vader_scores(data["feedbacktext"][row.name], "compound"),axis=1)
data

OUTPUT是:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM