0

当我在下面运行此代码时,它返回'float' object has no attribute 'encode' 我不确定我做错了什么,但我想获取标题的 VADER 情绪值(在一个大数据框中)但我不确定我哪里出错了,或者如何转换使对象可迭代的变量类型。然后将“复合”分数附加到数据框中。我尝试过迭代代码,例如:

pd.concat([bitcoin,bitcoin['Title'].apply(lambda r : pd.Series(analyzer.polarity_scores(r)))],axis=1) score_compound = bitcoin['Title'].apply(lambda r : analyzer.polarity_scores(r)['compound'])

import nltk
import pandas as pd

analyzer = SentimentIntensityAnalyzer()
bitcoin = pd.read_csv("Subreddit_Bitcoin_2021.csv")

score_compound = []

for i in range(0, bitcoin.shape[0]):
               score = analyzer.polarity_scores(bitcoin.iloc[i][1])
               score1 = score['compound']
               score_compound.append(score1)```


4

1 回答 1

0

没有您的数据可以处理,很难知道。我看到您在其他地方发布了相同的问题和一些数据,因此我对其进行了测试:

 index                                               text
0      0  I can’t believe Bitcoin is going to hit 100k b...
1      1  What new Bitcoin related project are you the m...
2      2  Yin decline is about to end! Historical data s...
3      3  If you discovered a way to model turning $100 ...
4      4  Happy New Year and some nice Gains !! ...

并完成您的代码(如需进一步通知,请分享您导入的库):

from nltk import *
import pandas as pd
import vader
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()

bitcoin = df

score_compound = []

for i in range(0, bitcoin.shape[0]):
               score = analyzer.polarity_scores(bitcoin.iloc[i][1])
               score1 = score['compound']
               score_compound.append(score1)
                
                
score_compound  

返回:

[0.0258, 0.4005, 0.0, 0.6199, 0.9421]
于 2022-01-24T12:46:29.880 回答