这是新事物,但尝试创建一个循环,将为数据框的每一行应用1,0情感得分
def my_sentiment_analyzer(documents):
preds = np.zeros(len(documents))
for i,doc in enumerate(documents):
scores=sid.polarity_scores(documents)
if scores["compound"] >= 0.05:
preds[i] = 1
else:
preds[i] = 0
return preds
当我在df上运行
preds = my_sentiment_analyzer(df['text'])
我知道
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-68-e1ccf893746d> in <module>
----> 1 preds = my_sentiment_analyzer(df['text'])
<ipython-input-67-58debf7737a4> in my_sentiment_analyzer(documents)
5
6 for i,doc in enumerate(documents):
----> 7 scores=sid.polarity_scores(documents)
8
9 if scores["compound"] >= 0.05:
C:\programdata\Anaconda3\lib\site-packages\nltk\sentiment\vader.py in polarity_scores(self,text)
351 valence.
352 """
--> 353 sentitext = SentiText(text)
354 # text,words_and_emoticons,is_cap_diff = self.preprocess(text)
355
C:\programdata\Anaconda3\lib\site-packages\nltk\sentiment\vader.py in __init__(self,text)
282 def __init__(self,text):
283 if not isinstance(text,str):
--> 284 text = str(text.encode('utf-8'))
285 self.text = text
286 self.words_and_emoticons = self._words_and_emoticons()
C:\programdata\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self,name)
5177 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5178 return self[name]
-> 5179 return object.__getattribute__(self,name)
5180
5181 def __setattr__(self,name,value):
AttributeError: 'Series' object has no attribute 'encode'
我们将不胜感激!