Count occurrences of inf in pandas dataframe

Viewed 3827

We can count occurrences of nan with df.isna().count() Is there is a similar function to count inf?

3 Answers

This worked for me:

number_inf = df[df == np.inf].count()

use np.isinf()

df = pd.DataFrame({'data' : [0,0,float('inf'),float('inf')]})


print(df)

   data
0   0.0
1   0.0
2   inf
3   inf


df.groupby(np.isinf(df['data'])).count()

       data
data       
False     2
True      2

you can use isinf() and ravel() in one line

# Pandas Series.ravel() function returns the flattened underlying data as an ndarray.
np.isinf(df["Col"]).values.ravel().sum()
Related