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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

it doesn't work, because the type of the columns is int: ValueError: cannot convert float NaN to integer
Well, then you'll have to convert the data to floats. You can't use NaN with integer data structures. See stackoverflow.com/questions/11548005/… .
so you should explain this in your answer, in the moment the answer doesn't fit to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.