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 1.9k times
20
Josh
- 10,579
- 17
- 52
- 85
-
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
- 83,134
- 124
- 340
- 545
-
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 NANs and only drop the infs)
CT Zhu
- 49,083
- 16
- 110
- 125
-
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