If I have a DataFrame:
myDF = DataFrame(data=[[11,11],[22,\'2A\'],[33,33]], columns = [\'A\',\'B\'])
Gives the following dataframe (Starting ou
much better/faster to do:
In [1]: myDF = DataFrame(data=[[11,11],[22,'2A'],[33,33]], columns = ['A','B'])
In [2]: myDF.convert_objects(convert_numeric=True)
Out[2]:
A B
0 11 11
1 22 NaN
2 33 33
[3 rows x 2 columns]
In [3]: myDF.convert_objects(convert_numeric=True).dtypes
Out[3]:
A int64
B float64
dtype: object
This is a vectorized method of doing just this. The coerce
flag say to mark as nan
anything that cannot be converted to numeric.
You can of course do this to a single column if you'd like.