4

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
Usama Abdulrehman
  • 1,041
  • 3
  • 11
  • 21
Bing Qian
  • 89
  • 1
  • 7
  • 1
    Should be marked as duplicate to http://stackoverflow.com/questions/18434208/pandas-converting-to-numeric-creating-nans-when-necessary – pansen Mar 27 '17 at 14:57

2 Answers2

10

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

EdChum
  • 376,765
  • 198
  • 813
  • 562
4

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
piRSquared
  • 285,575
  • 57
  • 475
  • 624