仅在 Pandas 中保留有限条目

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

Keep finite entries only in Pandas

pythonpandas

提问by Josh

In Pandas, I can use df.dropna()to drop any NaNentries. Is there anything similar in Pandas to drop non-finite (e.g. Inf) entries?

在 Pandas 中,我可以使用df.dropna()删除任何NaN条目。Pandas 中是否有类似的东西可以删除非有限(例如Inf)条目?

回答by Amelio Vazquez-Reina

You can use:

您可以使用:

with pd.option_context('mode.use_inf_as_null', True):
   df = df.dropna()

回答by rafaelvalle

df[np.isfinite(df) | np.isnan(df)]

回答by CT Zhu

You can use .dropna()after a DF[DF==np.inf]=np.nan, (unless you still want to keep the NANs and only drop the infs)

您可以.dropna()在 a 之后使用DF[DF==np.inf]=np.nan(除非您仍然想保留NANs 并且只删除infs)