Python Forum
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item()
#1
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().
Reply
#2
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... ).
Reply
#3
line 39 needs to be modified:
if pd.Series(df['compound'] > 0).all():
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 5,819 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Bool Object is not Subscriptable quest 1 4,186 May-02-2021, 11:12 AM
Last Post: Yoriz
  Python ValueError The value of a Series is abmbiguous nhl66pens 2 2,259 Oct-17-2020, 02:46 PM
Last Post: jefsummers
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() Rejoice 6 3,307 Aug-25-2020, 03:50 PM
Last Post: Rejoice
  bool b = (num == 100) this doesn't work? MelonMusk 2 2,018 Jun-12-2020, 02:18 AM
Last Post: bowlofred
  Convert quarterly time series to monthly time series donnertrud 1 5,203 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty
  ValueError - arg is an empty sequence jojotte 2 5,392 Dec-31-2018, 10:07 AM
Last Post: jojotte
  bool PreservedKillich 6 3,266 Aug-01-2018, 03:09 AM
Last Post: Skaperen
  [split] Set bool variable RedSkeleton007 1 2,732 Oct-22-2017, 09:28 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020