I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:

data.loc[:,'column_name'] = [char.replace(',', '') for char in 

but I am getting an error saying:

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

The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.

  • 1
    $\begingroup$ look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string. $\endgroup$
    – Valentas
    Feb 19, 2019 at 9:09
  • $\begingroup$ The question is about programming, not about Data Science, it is better if you move your question to StackOverflow. $\endgroup$
    – omar
    Feb 19, 2019 at 9:33

2 Answers 2


In pandas the object type is used when there is not a clear distinction between the types stored in the column.

So, I guess that in your column, some objects are float type and some objects are str type. Or maybe, you are also dealing with NaN objects, NaN objects are float objects.

a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str type or just make the type conversion of the column you are dealing with.

b) After that, you can apply the string changes and/or deal with the NaN objects.

c) Finally, you transform your column into float type`.


Maybe it's a very rudimentary method but I would just do

listt = []
for i in data['column_name']:
data['FloatData'] = listt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.