'float' object has no attribute 'strip'

Pumpkin C picture Pumpkin C · Aug 22, 2017 · Viewed 15.8k times · Source

I want to clean one column of my df['emp_length'] [shown in the screen shot]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

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!

Answer

MaxU picture MaxU · Aug 22, 2017

UPDATE: removing all non-digits:

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

old answer:

IIUC:

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