Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert non-numbers in dataframe to NaN (numpy)?

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
like image 724
Bing Qian Avatar asked Dec 11 '22 12:12

Bing Qian


2 Answers

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

like image 165
EdChum Avatar answered Dec 13 '22 01:12

EdChum


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
like image 41
piRSquared Avatar answered Dec 13 '22 01:12

piRSquared