Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep finite entries only in Pandas

Tags:

python

pandas

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?

like image 483
Josh Avatar asked Apr 02 '14 00:04

Josh


People also ask

How do I get rid of infinite value in pandas?

By using replace() & dropna() methods you can remove infinite values from rows & columns in pandas DataFrame. Infinite values are represented in NumPy as np. inf & -np. inf for negative values.

Does pandas Tolist preserve order?

The order of elements in a pandas Series (i.e., a column in a pandas DataFrame) will not change unless you do something that makes it change. And the order of a python list is guaranteed to reflect insertion order (SO thread). So yes, df[0].


3 Answers

You can use:

with pd.option_context('mode.use_inf_as_null', True):
   df = df.dropna()
like image 186
Amelio Vazquez-Reina Avatar answered Oct 22 '22 22:10

Amelio Vazquez-Reina


df[np.isfinite(df) | np.isnan(df)]
like image 30
rafaelvalle Avatar answered Oct 22 '22 21:10

rafaelvalle


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)

like image 28
CT Zhu Avatar answered Oct 22 '22 21:10

CT Zhu