Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'float' object has no attribute 'strip'

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!

like image 513
Pumpkin C Avatar asked Aug 22 '17 19:08

Pumpkin C


1 Answers

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('+')
like image 136
MaxU - stop WAR against UA Avatar answered Sep 23 '22 00:09

MaxU - stop WAR against UA