Python Forum

Full Version: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to calculate Accuracy based on the new compound column. The if statement is giving me an error.
Error is

# Importing Libraries 
import numpy as np   
import pandas as pd
import nltk
#nltk.download('vader_lexicon')

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
pos_count = 0
pos_correct = 0

neg_count = 0
neg_correct = 0

# creating object of SentimentIntensityAnalyzer
  
# Import dataset 
df = pd.read_csv("../Hotel_Reviews_SA.tsv", delimiter = '\t')

#df = pd.read_csv('../Restaurant_Reviews.tsv',sep='\\t')
df.head()

df.dropna(inplace=True)

sid.polarity_scores(df.iloc[0]['Review'])
#vs = sid.polarity_scores(df.iloc[0]['Review'])
df['scores'] = df['Review'].apply(lambda review:sid.polarity_scores(review))

df.head()

df['compound'] = df['scores'].apply(lambda d:d['compound'])


df.head()

df['score'] = df['compound'].apply(lambda score: 'pos' if score >=0 else 'neg')

#new code
if df['compound'] > 0:
    pos_correct += 1
    pos_count +=1
#end of new code

#new code
#print("Positive accuracy = {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count))
#print("Negative accuracy = {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count)
Data head is;
Rating Review Liked scores compound score
0 30 A major gripe however is that the on site res... 0 {'neg': 0.091, 'neu': 0.909, 'pos': 0.0, 'comp... -0.4199 neg
1 30 According to reception staff the hotel is wor... 0 {'neg': 0.141, 'neu': 0.784, 'pos': 0.074, 'co... -0.7572 neg
2 20 As Black person I was treated to the poorest ... 0 {'neg': 0.109, 'neu': 0.856, 'pos': 0.035, 'co... -0.7184 neg
3 30 Ask member of staff for coffee/tea, otherwise... 0 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
4 20 At every place except this hotel we were give... 0 {'neg': 0.065, 'neu': 0.896, 'pos': 0.04, 'com... -0.1779 neg

Error is
Error:
ValueError Traceback (most recent call last) <ipython-input-20-bcc97e606198> in <module> 37 38 ---> 39 if df['compound'] > 0: 40 pos_correct += 1 41 pos_count +=1 ~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self) 1553 "The truth value of a {0} is ambiguous. " 1554 "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format( -> 1555 self.__class__.__name__ 1556 ) 1557 ) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Unfortunately, I don't understood what you tried to do, but the error occurred because of pandas.Series/pandas.DataFrame objects could not be coerced to a Boolean value (The if-statement tries to convert your df['compound'] to Boolean, but don't know how... ).
line 39 needs to be modified:
if pd.Series(df['compound'] > 0).all():