如何解决“浮动”对象没有“编码”属性

问题描述

我正在计算酒店评论数据集的极性并为此代码出错:

df_final['neg'] = df_final['Reviews'].apply(lambda x:sia.polarity_scores(x)['neg'])
df_final['neu'] = df_final['Reviews'].apply(lambda x:sia.polarity_scores(x)['neu'])
df_final['pos'] = df_final['Reviews'].apply(lambda x:sia.polarity_scores(x)['pos'])
df_final['compound'] = df_final['Reviews'].apply(lambda x:sia.polarity_scores(x)['compound'])

错误

 AttributeError                            Traceback (most recent call last)
      <ipython-input-33-a5fdaf2a6c3e>      in <module>
  ----> 1 df['neg'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['neg'])
  2 df['neu'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['neu'])
  3 df['pos'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['pos'])
  4 df['compound'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['compound'])

~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self,func,convert_dtype,args,**kwds)
   4198             else:
   4199                 values = self.astype(object)._values
   -> 4200                 mapped = lib.map_infer(values,f,convert=convert_dtype)
      4201 
      4202         if len(mapped) and isinstance(mapped[0],Series):

   pandas\_libs\lib.pyx        in        pandas._libs.lib.map_infer()

    <ipython-input-33-a5fdaf2a6c3e>               
     in<lambda>(x)
     ----> 1 df['neg'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['neg'])
  2 df['neu'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['neu'])
  3 df['pos'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['pos'])
  4 df['compound'] = df['Reviews'].apply(lambda x:sid.polarity_scores(x)['compound'])

 ~\anaconda3\lib\site-packages\nltk\sentiment\vader.py in polarity_scores(self,text)
358         """
359         # text,words_and_emoticons,is_cap_diff = self.preprocess(text)
    --> 360         sentitext = SentiText(text,self.constants.PUNC_LIST,361                               self.constants.REGEX_REMOVE_PUNCTUATION)
362         sentiments = []

    ~\anaconda3\lib\site-packages\nltk\sentiment\vader.py in __init__(self,text,punc_list,regex_remove_punctuation)
268     def __init__(self,regex_remove_punctuation):
269         if not isinstance(text,str):
      --> 270             text = str(text.encode("utf-8"))
271         self.text = text
272         self.PUNC_LIST = punc_list

   AttributeError: 'float' object has no attribute 'encode'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)