# How to count non-nan elements in NumPy array

In this post, we are going to understand How to count non-nan elements in NumPy array for example by using numpy library function isnan(). The nan is not a number or missing value. To run the below programs Numpy must install on the system and imported it in programs using “import numpy as np”

### How to count non-nan Value in NumPy array

To count not nan values in 2D numpy array the ~np.isnan() function is used with Not operator. The np. isnan() function returns a boolean array of True is equal to 1 where the value is nan and False is equal to 0 for non-nan values. The Not operator reverses the result. So finally we get all the non-nan values in whole the Numpy array. because we did not pass the axis.The np.count_nonzero() is used to count non-Nan values

### Python Program to count non-nan value NumPy array

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.nan, 8, 9, np.nan],
[11, 12, np.nan, 14, 15,]])

print('count of non-nan value in numpy array:',np.count_nonzero(~np.isnan(nparr)))
```

Output

```count of non-nan value in numpy array: 12
```

### 2. How to count non-nan Value in NumPy array column-wise

In this python program to count the non-nan values column-wise, we have passed axis=0 to the isnan() function of the numpy library.

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.nan, 8, 9, np.nan],
[11, 12, np.nan, 14, 15,]])

print('count of non-nan value in numpy array column-wise:',np.count_nonzero(~np.isnan(nparr), axis=0))
```

Output

```count of non-nan value in numpy array column-wise: [3 2 2 3 2]
```

### 3. count non-nan Value in NumPy array rows-wise

In this python program to count the non-nan values row-wise, we have passed argument axis=1 to the isnan() function

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.nan, 8, 9, np.nan],
[11, 12, np.nan, 14, 15,]])

print('count of non-nan value in numpy array row-wise:',np.count_nonzero(~np.isnan(nparr), axis=1))
```

Output

```count of non-nan value in numpy array column-wise: [5 3 4]
```

### 4. count non-nan elements in NumPy array using np.sum()

In this Python program, we have used the np. isnan() function along with Not operator to find all non-nan values in the whole numpy array regardless of the axis. Finally used the np.sum() instead of np.count_nonzero() to count all non-nan values.

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.nan, 8, 9, np.nan],
[11, 12, np.nan, 14, 15,]])

print('count of non-nan value in numpy array:',np.sum(~np.isnan(nparr)))
```

Output

```count of non-nan value in numpy array: 12
```

### 5. np.sum() to count non-nan elements in NumPy array column-wise

In this python program example, We have used the np. isnan() function along with Not operator along with axis=0 to find all non-nan values column-wise in numpy array

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.nan, 8, 9, np.nan],
[11, 12, np.nan, 14, 15,]])

print('count of non-nan value in numpy array column-wise:',np.sum(~np.isnan(nparr), axis=0))
```

Output

```count of non-nan value in numpy array column-wise: [3 2 2 3 2]
```

### 6. np.sum() to count non-nan elements in NumPy array row-wise

To find all the non-nan values row-wise in numpy array we have passed axis=1 to numpy np.isnan() function and used np.sum() to count the number of non-nan value.

```import numpy as np
nparr = np.array([[1, 2, 3, 4, 5],
[6, np.nan, 8, 9, np.nan],
[11, 12, np.nan, 14, 15,]])

print('count of non-nan value in numpy array row-wise:',np.sum(~np.isnan(nparr), axis=1))
```

Output

```count of non-nan value in numpy array column-wise: [5 3 4]
```

### Summary

In this post, we have learned how to count non-nan elements in NumPy array by using numpy library isnan(),np.sum(),np.count_nonzero() function with examples.