pythonpandasdataframeiterationvader

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


Solution

  • 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))