coder888
coder888

Reputation: 33

Cant seem to iterate over a column to assign sentiment values

I have tried iterating over rows in my dataframe to get sentimental values. My code is:

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

analyzer = SentimentIntensityAnalyzer()

df['Sentiment Values'] = df['Comments'].apply(lambda Comments: analyzer.polarity_scores(Comments))`

but it returns

'float' object has no attribute 'encode'

My df is:

  Comments

1 The main thing is the price appreciation of the token (this determines the gains or losses more 
  than anything). Followed by the ecosystem for the liquid staking asset, the more opportunities 
  and protocols that accept the asset as collateral, the better. Finally, the yield for staking 
  comes into play.

2 No problem. I’m the same. Good to hold both for sure!

3 I understood most of that. Thank you.

4 I could be totally wrong, but sounds like destroying an asset and claiming a loss, which I 
  believe is fraudulent. Like someone else said, get a tax guy - for this year anyway and then 
  you'll know for sure. Peace of mind has value too.

EDIT: Image of Data

Upvotes: 0

Views: 292

Answers (2)

tdpu
tdpu

Reputation: 703

I was able to execute the code using a .csv file adapted from your data.

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

analyzer = SentimentIntensityAnalyzer()

df = pd.read_csv('df.csv', delimiter='_')
df['Sentiment Values'] = df['Comments'].apply(lambda Comments: analyzer.polarity_scores(Comments))

Note I've used an underscore as delimiter, which may not be totally robust depending on your input data.

In the dataframe, there is only a single column of comments. It looks like you may have included indices (1,2,3,4...) which could be the source of your error.

df.csv:

Comments
The main thing is the price appreciation of the token (this determines the gains or losses more than anything). Followed by the ecosystem for the liquid staking asset, the more opportunities and protocols that accept the asset as collateral, the better. Finally, the yield for staking comes into play.
No problem. I’m the same. Good to hold both for sure!
I understood most of that. Thank you.
I could be totally wrong, but sounds like destroying an asset and claiming a loss, which I believe is fraudulent. Like someone else said, get a tax guy - for this year anyway and then you'll know for sure. Peace of mind has value too.

Upvotes: 0

user17242583
user17242583

Reputation:

I'm not able to reproduce - probably because the error is happening later down the dataframe than you've sent here.

I'm guessing the issue is that you've got some non-strings (floats, specifically) in your Comments columns. Probably you should examine them and remove them, but you can also just convert them to strings before sentiment analysis with .astype(str):

df['Sentiment Values'] = df['Comments'].astype(str).apply(lambda Comments: analyzer.polarity_scores(Comments))

Upvotes: 3

Related Questions