简体   繁体   English

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

[英]'float' object has no attribute 'strip'

I want to clean one column of my df['emp_length'] [shown in the screen shot] 1 我想清理我的df ['emp_length']的一列[如屏幕截图所示] 1

but when I use 但是当我使用

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

to remove thing i dont want. 删除我不想要的东西。 It gave me an error: 它给了我一个错误:

'float' object has no attribute 'lstrip'

However, the type shows object instead of float. 但是,该类型显示的是对象而不是float。 I tried .remove too but gave me the same error. 我也尝试过.remove,但是给了我同样的错误。 I also tried 我也试过

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

to change df_10v['emp_length'] in to string and then strip, but it does not work either. 将df_10v ['emp_length']更改为字符串,然后剥离,但这也不起作用。 Anybody know how to solve this? 有人知道如何解决这个问题吗? Thank you! 谢谢!

UPDATE: removing all non-digits: 更新:删除所有非数字:

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

old answer: 旧答案:

IIUC: IIUC:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM