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.
fillna() function of Pandas conveniently handles missing values. Using fillna(), missing values can be replaced by a special value or an aggreate value such as mean, median. Furthermore, missing values can be replaced with the value before or after it which is pretty useful for time-series datasets.
The first sentinel value used by Pandas is None , a Python 'object' data that is most often used for missing data in Python code.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With