Reputation: 89
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
Upvotes: 4
Views: 3214
Reputation: 294258
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
Upvotes: 4
Reputation: 394031
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
Upvotes: 10