仅在 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
Keep finite entries only in Pandas
提问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)

