Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 221
DanB Avatar asked Aug 09 '12 18:08

DanB


People also ask

Which method of Pandas allows you to replace missing values?

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.

What values does Pandas normally use for missing data?

The first sentinel value used by Pandas is None , a Python 'object' data that is most often used for missing data in Python code.


1 Answers

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

like image 97
BrenBarn Avatar answered Oct 31 '22 02:10

BrenBarn