【问题标题】:AttributeError: 'int' object has no attribute 'split' for pandasAttributeError:“int”对象没有熊猫的“split”属性
【发布时间】:2021-09-09 21:43:08
【问题描述】:

AttributeError: 'int' object has no attribute '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 列是对象类型,它返回的 int 对象没有属性拆分

【问题讨论】:

  • 试试df['Content'].str.split().apply(len)

标签: python python-3.x pandas dataframe integer


【解决方案1】:

你可以试试:

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

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

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

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

【讨论】:

  • 有些开发者一般更喜欢统计空格数' ',所以我觉得第二种方案更好
猜你喜欢
  • 2015-04-30
  • 2021-04-05
  • 2017-07-02
  • 2016-01-31
  • 2016-09-19
  • 2020-09-14
  • 2021-07-25
  • 2019-03-18
  • 1970-01-01
相关资源
最近更新 更多