-4

I am trying to run sentiment analysis on a selection of a data set, but every time I do I get this error: KeyError: 0

For reference, this is the code I am working with:

OC = df[df["text"].str.contains("Obamacare")]

from textblob import TextBlob
import re

def clean_tweet(tweet):
    return " ".join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
def analize_sentiment(tweet):
    analysis = TextBlob(clean_tweet(tweet))
    if analysis.sentiment.polarity > 0:
        return 1
    elif analysis.sentiment.polarity == 0:
        return 0
    else:
        return -1

df["sentiment"] = np.array([ analize_sentiment(tweet) for tweet in df["text"]])
pos_tweets = [tweet for index, tweet in enumerate(OC['text']) if OC['sentiment'][index] > 0]
neu_tweets = [ tweet for index, tweet in enumerate(OC['text']) if OC['sentiment'][index] == 0]
neg_tweets = [ tweet for index, tweet in enumerate(OC['text']) if OC['sentiment'][index] < 0]

It's after I try to run the pos_tweets, neu_tweets, neg_tweets that I keep getting Key Error: 0

Anh Pham
  • 695
  • 1
  • 6
  • 21
kbenne
  • 1

1 Answers1

0

I'm not sure what you're enumerating or why that is part of a sentiment analysis. This is how I did it...

def clean(tweet):
    return " ".join(re.sub("(@[A-Za-z0-9]+) | ([^0-9A-Za-z\t]) | (w+:\/\/\s+)", " ", tweet).split())

def sentiment_analysis(tweet):
    analysis = TextBlob(clean(tweet))
if analysis.sentiment.polarity > 0:
    return 1
elif analysis.sentiment.polarity == 0:
    return 0
else:
    return -1

df["sentiment"] = np.array([sentiment_analysis(tweet) for tweet in 
df["text"]])

df["OC"] = df.text.str.contains("obamacare", case = False)
df2 = df.loc[df["OC"] == True]
df2.sentiment.value_counts() 

I basically just ran your list comprehension against the entire df, and then parsed.