pandas 'float' 对象没有属性 'strip'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45825380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'float' object has no attribute 'strip'
提问by Pumpkin C
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. I tried .remove too but gave me the same error. I also tried
但是,该类型显示对象而不是浮动。我也试过 .remove 但给了我同样的错误。我也试过
df['column'] = df['column'].astype('str')
to change df_10v['emp_length'] in to string and then strip, but it does not work either. Anybody know how to solve this? Thank you!
将 df_10v['emp_length'] 更改为字符串然后剥离,但它也不起作用。有谁知道如何解决这个问题?谢谢!
回答by MaxU
UPDATE:removing all non-digits:
更新:删除所有非数字:
df_10v['emp_length'] = df_10v['emp_length'].astype(str).str.replace('\D+', '')
old answer:
旧答案:
IIUC:
IUC:
df_10v['emp_length'] = df_10v['emp_length'].astype(str).str.lstrip('<').str.rstrip('+')