DanB
DanB

Reputation: 4015

Converting data to missing in pandas

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.

Upvotes: 10

Views: 5756

Answers (1)

BrenBarn
BrenBarn

Reputation: 251355

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.)

Upvotes: 11

Related Questions