似乎无法遍历列来分配情绪值

Cant seem to iterate over a column to assign sentiment values

我尝试遍历我的数据框中的行来获取情感值。 我的代码是:

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

但是 returns

'float' object has no attribute 'encode'

我的 df 是:

  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.

编辑:

我无法重现 - 可能是因为错误发生在数据帧的后面,而不是你发送到这里的时间。

我猜问题是您的 Comments 列中有一些 non-strings(特别是浮点数)。也许你应该检查它们并删除它们,但你也可以在使用 .astype(str):

进行情绪分析之前将它们转换为字符串
df['Sentiment Values'] = df['Comments'].astype(str).apply(lambda Comments: analyzer.polarity_scores(Comments))

我能够使用根据您的数据改编的 .csv 文件执行代码。

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

请注意,我使用下划线作为分隔符,根据您的输入数据,下划线可能不完全可靠。

在数据框中,只有一列评论。看起来您可能包含了索引 (1,2,3,4...),这可能是错误的来源。

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.