In Pandas, I can use df.dropna()
to drop any NaN
entries. Is there anything similar in Pandas to drop non-finite (e.g. Inf
) entries?
Asked
Active
Viewed 2.3k times
20

Josh
- 11,979
- 17
- 60
- 96
-
Possible duplicate of [dropping infinite values from dataframes in pandas?](https://stackoverflow.com/questions/17477979/dropping-infinite-values-from-dataframes-in-pandas) – chepyle Jun 07 '18 at 21:36
3 Answers
27
You can use:
with pd.option_context('mode.use_inf_as_null', True):
df = df.dropna()

Amelio Vazquez-Reina
- 91,494
- 132
- 359
- 564
-
this causes my dataframe to go from 130 rows to 0. The na/inf issue I'm investigating is a column of all blanks, i believe – 3pitt Jan 22 '18 at 16:44
-
You can specify the axis you wish to drop NaNs over as a parameter to dropna – Ayrton Bourn Mar 21 '21 at 00:48
-
3
You can use .dropna()
after a DF[DF==np.inf]=np.nan
, (unless you still want to keep the NAN
s and only drop the inf
s)

CT Zhu
- 52,648
- 17
- 120
- 133
-
Thanks. Surprisingly `df.drop(df==np.inf)` doesn't work. Any thoughts why? – Josh Apr 02 '14 at 00:19
-
2I think, as the docstring goes: `.dropna(self, axis=0, how='any', thresh=None, subset=None)`, `df==np.inf` will get passed as `axis` argument, with should raise an exception. – CT Zhu Apr 02 '14 at 00:25