20

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?

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 Answers3

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
9
df[np.isfinite(df) | np.isnan(df)]
rafaelvalle
  • 6,683
  • 3
  • 34
  • 36
3

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)

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
  • 2
    I 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