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

时间:2017-08-22 19:19:03

标签: python pandas dataframe strip data-cleaning

我想清理我的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']更改为字符串然后删除,但它也不起作用。谁知道怎么解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:5)

更新:删除所有非数字:

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('+')
相关问题