How to convert non-numbers in DataFrame to NaN (numpy)? For example, here is a DataFrame:
a    b
--------
10   ...
4    5
...  6
How to covert it to:
a    b
--------
10   NaN
4    5
NaN  6
                IIUC you can just do
df = df.apply(lambda x: pd.to_numeric(x, errors='coerce') )
This will force the duff values to NaN, note that the presence of NaN will change the dtype to float as NaN cannot be represented by int
In [6]:
df = df.apply(pd.to_numeric, errors='coerce')
df
Out[6]:
      a    b
0  10.0  NaN
1   4.0  5.0
2   NaN  6.0
The lambda isn't necessary but it's more readable IMO
You can can also stack then unstack the dataframe
pd.to_numeric(df.stack(), errors='coerce').unstack()
      a    b
0  10.0  NaN
1   4.0  5.0
2   NaN  6.0
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With