ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() (/thread-22779.html) |
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - bongielondy - Nov-27-2019 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
RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - scidam - Nov-27-2019 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... ). RE: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() - ThomasL - Nov-27-2019 line 39 needs to be modified: if pd.Series(df['compound'] > 0).all(): |