10

I have a DataFrame with a mix of 0's and other numbers. I would like to convert the 0's to missing.

For example, I am looking for the command that would convert

In [618]: a=DataFrame(data=[[1,2],[0,1],[1,2],[0,0]])

In [619]: a
Out[619]: 
   0  1
0  1  2
1  0  1
2  1  2
3  0  0

to

In [619]: a
Out[619]: 
   0   1
0  1   2
1  NaN 1
2  1   2
3  NaN NaN

I tried pandas.replace(0, NaN), but I get an error that NaN is not defined. And I don't see anywhere to import NaN from.

piRSquared
  • 285,575
  • 57
  • 475
  • 624
DanB
  • 3,755
  • 4
  • 22
  • 22

1 Answers1

11

Just do from numpy import nan. (You will have to convert your DataTable to float type, because you can't use NaN in integer arrays.)

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • 2
    it doesn't work, because the type of the columns is int: `ValueError: cannot convert float NaN to integer` – bmu Aug 09 '12 at 18:44
  • 2
    Well, then you'll have to convert the data to floats. You can't use `NaN` with integer data structures. See http://stackoverflow.com/questions/11548005/numpy-or-pandas-keeping-array-type-as-integer-while-having-a-nan-value . – BrenBarn Aug 09 '12 at 18:46
  • 3
    so you should explain this in your answer, in the moment the answer doesn't fit to the question. – bmu Aug 09 '12 at 18:50