无法使用 Pandas 在 Python 中将 0 替换为 nan

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49575897/
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-08-19 19:08:56  来源:igfitidea点击:

Can't replace 0 to nan in Python using Pandas

pythonpandasdataframe

提问by Ilia Chigogidze

I have dataframe with only 1 column. I want to replace all '0' to np.nan but I can't achieve that.

我的数据框只有 1 列。我想将所有 '0' 替换为 np.nan 但我无法实现。

dataframe is called area. I tried:

数据框称为区域。我试过:

area.replace(0,np.nan)
area.replace(to_replace=0,np.nan)
area.replace(to_replace=0,value=np.nan)

area.replace('0',np.nan)

What should I do?

我该怎么办?

回答by zipa

You can set inplaceto True(default is False):

您可以设置inplaceTrue(默认为False):

area.replace(0, np.nan, inplace=True)

See examples in docs.

请参阅文档中的示例。

回答by drec4s

You need to do:

你需要做:

area = area.replace(0, np.nan)