pandas 熊猫:将空单元格替换为 0

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

Pandas: replace empty cell to 0

pythonpandas

提问by ldevyataykina

I have a data frame resultsthat contains empty cells and I would like to replace all empty cells with 0.

我有一个results包含空单元格的数据框,我想用 0 替换所有空单元格。

So far I have tried using pandas' fillna:

到目前为止,我已经尝试过使用 pandas' fillna

result.fillna(0)

and replace:

replace

result.replace(r'\s+', np.nan, regex=True)

However, both with no success.

然而,两者都没有成功。

回答by elelias

You are creating a copy of the dataframe but the original one is not keeping the changes, you need to specify "inplace=True" if you want the dataframe to persist the changes

您正在创建数据框的副本,但原始副本没有保留更改,如果您希望数据框保留更改,则需要指定“inplace=True”

result.fillna(0, inplace=True)