2012-08-09 181 views
10

我有一個DataFrame與0和其他數字的混合。我想將0轉換爲丟失。將數據轉換爲熊貓缺失

例如,我在尋找能轉換

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 

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

我試圖pandas.replace(0,NAN)的命令,但我得到一個錯誤,楠沒有定義的。我沒有看到任何地方從NaN進口。

回答

10

只要做到from numpy import nan。 (您將不得不將數據錶轉換爲浮點類型,因爲您不能在整數數組中使用NaN。)

+2

它不起作用,因爲列的類型是int:'ValueError:無法將浮點型NaN轉換爲整數' – bmu 2012-08-09 18:44:56

+2

那麼,你將不得不將數據轉換爲浮點數。你不能使用具有整數數據結構的'NaN'。請參閱http://stackoverflow.com/questions/11548005/numpy-or-pandas-keeping-array-type-as-integer-while-having-a-nan-value。 – BrenBarn 2012-08-09 18:46:39

+3

所以你應該在你的答案中解釋這一點,在答案不適合問題的時刻。 – bmu 2012-08-09 18:50:55