pandas 将数据转换为熊猫中的缺失数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11889474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:47:25  来源:igfitidea点击:

Converting data to missing in pandas

pythonnumpypandas

提问by DanB

I have a DataFramewith a mix of 0's and other numbers. I would like to convert the 0's to missing.

我有一个DataFrame混合了 0 和其他数字的数字。我想将 0 转换为缺失。

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.

我试过 pandas.replace(0, NaN),但我收到一个错误,指出未定义 NaN。而且我看不到任何地方可以从中导入 NaN。

回答by BrenBarn

Just do from numpy import nan. (You will have to convert your DataTable to float type, because you can't use NaNin integer arrays.)

就做from numpy import nan。(您必须将 DataTable 转换为浮点类型,因为您不能NaN在整数数组中使用。)