'float' 对象没有属性 'strip'

'float' object has no attribute 'strip'

我想清理我的 df['emp_length'] 的一栏 [如屏幕截图所示]1

但是当我使用

df_10v['emp_length'] = df_10v['emp_length'].map(lambda x: x.lstrip('<').rstrip('+'))

删除我不想要的东西。它给了我一个错误:

'float' object has no attribute 'lstrip'

但是,类型显示对象而不是浮点数。 我也试过 .remove 但给了我同样的错误。 我也试过

df['column'] = df['column'].astype('str') 

把df_10v['emp_length']改成string,然后strip,也不管用。有人知道如何解决这个问题吗?谢谢!

更新: 删除所有非数字:

df_10v['emp_length'] = df_10v['emp_length'].astype(str).str.replace('\D+', '')

旧答案:

IIUC:

df_10v['emp_length'] = df_10v['emp_length'].astype(str).str.lstrip('<').str.rstrip('+')