从 pandas 的数据框中删除无限值? [英] dropping infinite values from dataframes in pandas?
本文介绍了从 pandas 的数据框中删除无限值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从熊猫DataFrame中删除nan和inf/-inf值而不重置mode.use_inf_as_null
的最快/最简单方法是什么?我希望能够使用dropna
的subset
和how
参数,除了inf
值被认为丢失,例如:
what is the quickest/simplest way to drop nan and inf/-inf values from a pandas DataFrame without resetting mode.use_inf_as_null
? I'd like to be able to use the subset
and how
arguments of dropna
, except with inf
values considered missing, like:
df.dropna(subset=["col1", "col2"], how="all", with_inf=True)
这可能吗?有没有办法告诉dropna
在其缺少值的定义中包括inf
?
is this possible? Is there a way to tell dropna
to include inf
in its definition of missing values?
推荐答案
The simplest way would be to first replace
infs to NaN:
df.replace([np.inf, -np.inf], np.nan)
,然后使用 dropna
:
and then use the dropna
:
df.replace([np.inf, -np.inf], np.nan).dropna(subset=["col1", "col2"], how="all")
例如:
In [11]: df = pd.DataFrame([1, 2, np.inf, -np.inf])
In [12]: df.replace([np.inf, -np.inf], np.nan)
Out[12]:
0
0 1
1 2
2 NaN
3 NaN
相同的方法适用于Series.
这篇关于从 pandas 的数据框中删除无限值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文