Python Forum
text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc
#1
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')
Reply
#2
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/
Reply
#3
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text frame dnabtuh 1 143 May-07-2025, 05:47 AM
Last Post: Larz60+
Exclamation URGENT: How to plot data from text file. Trying to recreate plots from MATLAB JamieAl 4 5,139 Dec-03-2023, 06:56 AM
Last Post: Pedroski55
  dictionary output to text file (beginner) Delg_Dankil 2 3,585 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  fixing error TypeError: 'float' object is not subscriptable programmingirl 1 2,369 Jan-28-2023, 08:13 PM
Last Post: deanhystad
  beginner having text based adventure trouble mrgee 2 2,806 Dec-16-2021, 05:07 AM
Last Post: buran
  Trouble downloading and using any text editors edwarmax001 1 2,533 Feb-20-2021, 05:36 PM
Last Post: Larz60+
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 11,823 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  HomeWork Python - Drawing window with text center. Voraman 8 4,523 Jan-09-2021, 06:53 PM
Last Post: Voraman
  Reading a text until matched string and print it as a single line cananb 1 2,719 Nov-29-2020, 01:38 PM
Last Post: DPaul
  AttributeError: 'str' object has no attribute 'size' russoj5 4 9,641 Nov-15-2020, 11:43 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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