farhawa farhawa - 3 months ago 11
Python Question

pandas.DataFrame set all string values to nan

I have a

pandas.DataFrame
that contain string, float and int types.

Is there a way to set all strings that cannot be converted to float to
NaN
?

For example:

A B C D
0 1 2 5 7
1 0 4 NaN 15
2 4 8 9 10
3 11 5 8 0
4 11 5 8 "wajdi"


to:

A B C D
0 1 2 5 7
1 0 4 NaN 15
2 4 8 9 10
3 11 5 8 0
4 11 5 8 NaN

Answer

Try

df.convert_objects(convert_numeric=True)
Comments