如何在不重置的情况下从中删除nan、inf和-inf值?DataFrame``mode.use_inf_as_null
nan
inf
-inf
DataFrame``mode.use_inf_as_null
我可以告诉dropna它inf在其定义中包含缺失值以便以下内容起作用吗?
dropna
df.dropna(subset=["col1", "col2"], how="all")
第一个replace()infs 为 NaN:
replace()
df.replace([np.inf, -np.inf], np.nan, inplace=True)
然后通过以下方式删除 NaN dropna():
dropna()
df.dropna(subset=["col1", "col2"], how="all", inplace=True)
例如:
>>> df = pd.DataFrame({"col1": [1, np.inf, -np.inf], "col2": [2, 3, np.nan]}) >>> df col1 col2 0 1.0 2.0 1 inf 3.0 2 -inf NaN >>> df.replace([np.inf, -np.inf], np.nan, inplace=True) >>> df col1 col2 0 1.0 2.0 1 NaN 3.0 2 NaN NaN >>> df.dropna(subset=["col1", "col2"], how="all", inplace=True) >>> df col1 col2 0 1.0 2.0 1 NaN 3.0
同样的方法也适用于Series。
Series