Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "AttributeError: 'float' object has no attribute 'replace'" error while replacing string

I have a column which has text in it. I have to replace (':',' ').

When I am running this code:

df["text"] = [x.replace(':',' ') for x in df["text"]]

I am facing the this error:

AttributeError: 'float' object has no attribute 'replace'

AttributeError                            Traceback (most recent call 
last)
<ipython-input-13-3c116e6f21e2> in <module>
  1 #df["text"] = df["text"].astype(str)
----> 2 df["text"] = [x.replace(':',' ') for x in df["text"]]

<ipython-input-13-3c116e6f21e2> in <listcomp>(.0)
  1 #df["text"] = data["NOTES_ENT"].astype(str)
----> 2 df["text"] = [x.replace(':',' ') for x in df["text"]]

AttributeError: 'float' object has no attribute 'replace'
like image 836
anil Avatar asked Apr 21 '26 16:04

anil


1 Answers

The error is pretty clear. you are trying to replace characters in a float. x probably is a float. you might want to do str(x) which gets you

f["text"] = [str(x).replace(':',' ') for x in df["text"]]

but i can't see a situation where a float contains a :

like image 144
jonathan Avatar answered Apr 23 '26 06:04

jonathan