AttributeError: 'int' object has no attribute 'split' for pandas

AttributeError: 'int' object has no attribute 'split' for pandas

AttributeError: 'int' 对象没有属性 'split'

数据是:

print(df)


    Content               Page no
0   My name is mark       3
1   My name is jeff       3
2   My name is bill       3

密码是:

df['doc_len'] = df['Content'].apply(lambda words: len(words.split()))

它返回的错误是:

AttributeError                            Traceback (most recent call last)
<ipython-input-26-7d2f1de16b3d> in <module>
----> 1 df['doc_len'] = df['Content'].apply(lambda words: len(words.split()))

~\t5\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()

<ipython-input-26-7d2f1de16b3d> in <lambda>(words)
----> 1 df['doc_len'] = df['Content'].apply(lambda words: len(words.split()))

AttributeError: 'int' object has no attribute 'split'

Content列是object类型返回int对象没有属性split

你可以试试:

通过 str.split()str.len():

df['doc_len']=df['Content'].str.split().str.len()

通过 str.count() 然后添加 1:

df['doc_len']=df['Content'].str.count(' ')+1