The simplest way would be to first replace()
infs to NaN:
df.replace([np.inf, -np.inf], np.nan, inplace=True)
and then use the dropna()
:
df.replace([np.inf, -np.inf], np.nan, inplace=True)
.dropna(subset=["col1", "col2"], how="all")
For example:
In [11]: df = pd.DataFrame([1, 2, np.inf, -np.inf])
In [12]: df.replace([np.inf, -np.inf], np.nan, inplace=True)
Out[12]:
0
0 1
1 2
2 NaN
3 NaN
The same method would work for a Series.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…