首页 > 解决方案 > TypeError:“float”类型的对象没有 len() - NLP

问题描述

我正在尝试创建一个具有文本长度的变量。我正在使用下面的代码:

messages['length']=messages['Review Text'].apply(len)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-fa1723bfb3a3> in <module>
----> 1 messages['length']=messages['Review Text'].apply(len)

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

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

TypeError: object of type 'float' has no len()

我应该怎么做才能解决这个问题?

标签: pythonpandasnlp

解决方案


仔细检查您的数据。该消息"TypeError: object of type 'float' has no len()"使我相信您的至少一个单元格'Review Text'不是文本值,而是浮点值。


推荐阅读