Trying to change pandas column dtype from str to float 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 
                                data.loc[:,'column_name']]


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.
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`.