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

Viewed 832

I'm trying to determine the sentiment score for customer feedback using VADER in python. The simple code below is running perfectly for individual feedback and returning a dictionary of negative, neutral, positive and compound score.

Code:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer


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

print(sentiment)

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

Now, I have a spreadsheet of 4k+ customer feedback. What I'm trying to do is to iterate through every feedback and add 4 new col as Negative_Score, Neutral_Score, Positive_Score and Compound_Score. I've written following code which is not giving desired result. Getting same score for every row. Any help will be appreciated.

Code:

    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
1 Answers

We can achieve this using lambda. It is better than iterating through the data frame rows. I have written a method vader_scores the sole function is to return the respectivite polarity score (pos/neg/neu/compound) from the feedback text.

The main reason your code returned the same polarity score for all rows was that you had used data["feedbacktext"] alone, which took the entire column and returned the resultant value for it. What I have done is, have picked the data["feedbacktext"] from every row using the index of the row and have populated its cell in Negative_Score , Neutral_Score, Positive_Score, and Compound_Score columns.

The sample data I had used was :

enter image description here

This is my code:

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

And the OUTPUT is:

enter image description here

Related