1

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

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

但它返回

'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.

编辑: 数据图像

4

2 回答 2

3

我无法重现 - 可能是因为错误发生在数据帧后面,而不是您在此处发送的。

我猜问题是你的Comments列中有一些非字符串(特别是浮点数)。可能您应该检查它们并删除它们,但您也可以在情绪分析之前将它们转换为字符串.astype(str)

df['Sentiment Values'] = df['Comments'].astype(str).apply(lambda Comments: analyzer.polarity_scores(Comments))
于 2022-01-30T17:11:14.743 回答
1

我能够使用根据您的数据改编的 .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.
于 2022-01-30T17:21:02.653 回答