Python Forum

Full Version: text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im trying to do a sentiment analysis of Twitter tweets on different topics. Thereby, some topics produce the following error message:

AttributeError: 'float' object has no attribute 'encode' 

What shall I do?

My code looks like this:

import pandas as pd
import numpy as np from nltk.sentiment.vader
import SentimentIntensityAnalyzer

tweets=pd.read_excel('Topic1.xlsx',header=0)

sid = SentimentIntensityAnalyzer()

for sentence in tweets.Tweet: tweets['Sentiment'] = tweets['Tweet'].apply(lambda sentence: sid.polarity_scores(sentence)['compound'])

tweets.to_excel('Topic1s.xlsx')
Please, post the full traceback, not just the last line, in error tags. Also post your full code if you have not done so (the code in thread title is not seen in your code snippet)
cross-posted at reddit 7 days ago
https://www.reddit.com/r/learnpython/com...ror_float/
Here is the full error message. The line from the title is part from this message.

Output:
Traceback (most recent call last):   File "<ipython-input-1-2b37aa124c89>", line 10, in <module>     tweets['Sentiment'] = tweets['Tweet'].apply(lambda sentence: sid.polarity_scores(sentence)['compound'])   File "C:\Anaconda\Anaconda3\lib\site-packages\pandas\core\series.py", line 2355, in apply     mapped = lib.map_infer(values, f, convert=convert_dtype)   File "pandas\_libs\src\inference.pyx", line 1569, in pandas._libs.lib.map_infer (pandas\_libs\lib.c:66440)   File "<ipython-input-1-2b37aa124c89>", line 10, in <lambda>     tweets['Sentiment'] = tweets['Tweet'].apply(lambda sentence: sid.polarity_scores(sentence)['compound'])   File "C:\Anaconda\Anaconda3\lib\site-packages\nltk\sentiment\vader.py", line 223, in polarity_scores     sentitext = SentiText(text)   File "C:\Anaconda\Anaconda3\lib\site-packages\nltk\sentiment\vader.py", line 158, in __init__     text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'encode'