pandas 如何用零替换numpy数组中的inf

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

How to replace inf in a numpy array with zero

pandas

提问by Drew Yallop

I am trying to eliminate an inf from a pandas DataFrame, caused by a division by zero. I have tried several techniques using both DataFrame and ndarray structures:

我正在尝试从 Pandas DataFrame 中消除由零除引起的 inf。我已经尝试了几种使用 DataFrame 和 ndarray 结构的技术:

df_fund['dly_retn'].replace(np.inf, 0)
na_fund['dly_retn'].replace(np.inf, 0)
na_dly_retn(~isfinite(na_dly_retn))=0

Taking the mean in every case results in "inf"

在每种情况下取​​平均值导致 "inf"

I have searched for two days without finding an answer to what should be a trivial problem.

我已经搜索了两天,但没有找到应该是微不足道的问题的答案。

回答by Boud

You have to save the operation in your dataframe. One way is to use the parameter inplace=True:

您必须将操作保存在数据框中。一种方法是使用参数inplace=True

df_fund['dly_retn'].replace(np.inf, 0, inplace=True)
na_fund['dly_retn'].replace(np.inf, 0, inplace=True)