Andrey Lukyanenko Andrey Lukyanenko - 2 months ago 9
Python Question

Can't convert column with pandas.to_numeric

I have a column of data, here is a snip of it:

a = data["hs_directory"]["lat"][:5]

0 40.67029890700047
1 40.8276026690005
2 40.842414068000494
3 40.71067947100045
4 40.718810094000446
Name: lat, dtype: object


I try to convert it to numerical with python, but fail:

pandas.to_numeric(a, errors='coerce')


This line does nothing, the dtype is still "object" and I can't do mathematical operations with the column. What am I doing wrong?

Answer

As discussed in the comments, let me post the answer for future readers:

try:

df1=pd.to_numeric(a,errors='coerce')