7

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!

cs95
  • 379,657
  • 97
  • 704
  • 746
Pumpkin C
  • 1,452
  • 6
  • 21
  • 27

1 Answers1

14

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('+')
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • Thank you so much. I am able to remove the '<' and 'year', but still cant remove '+'. i tried to remove year first and then +, but it does not work. df_10v['emp_length'] = df_10v['emp_length'].astype(str).str.lstrip('<').str.rstrip('years') df_10v['emp_length'] = df_10v['emp_length'].astype(str).str.rstrip('+') – Pumpkin C Aug 22 '17 at 20:01
  • @PumpkinC, please [post in your question](https://stackoverflow.com/posts/45825380/edit) a small (3-5 rows) reproducible data set and your desired data set. Please read [how to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your post correspondingly. – MaxU - stand with Ukraine Aug 22 '17 at 20:02
  • for those '10+ years', i cant remove the '+' – Pumpkin C Aug 22 '17 at 20:03
  • @PumpkinC, are you trying to remove everything except digits? – MaxU - stand with Ukraine Aug 22 '17 at 20:03
  • Yes. that's what i am trying to do – Pumpkin C Aug 22 '17 at 20:04
  • @PumpkinC, if you would post a small sample data set and your desired data set - I bet you would have a proper answer within 1-2 minutes... – MaxU - stand with Ukraine Aug 22 '17 at 20:07
  • got it! thank you! i am kind of new to all this.. still struggling how to ask right question. – Pumpkin C Aug 22 '17 at 20:17
  • It works. Thank you so much. and Thank you for your advice! – Pumpkin C Aug 22 '17 at 20:28