﻿ NumPy: Remove nan values from a given array - w3resource # NumPy: Remove nan values from a given array

## NumPy: Array Object Exercise-110 with Solution

Write a NumPy program to remove nan values from a given array.

Sample Solution:

Python Code:

``````import numpy as np
x = np.array([200, 300, np.nan, np.nan, np.nan ,700])
y = np.array([[1, 2, 3], [np.nan, 0, np.nan] ,[6,7,np.nan]] )
print("Original array:")
print(x)
print("After removing nan values:")
result = x[np.logical_not(np.isnan(x))]
print(result)
print("\nOriginal array:")
print(y)
print("After removing nan values:")
result = y[np.logical_not(np.isnan(y))]
print(result)
```
```

Sample Output:

```Original array:
[200. 300.  nan  nan  nan 700.]
After removing nan values:
[200. 300. 700.]

Original array:
[[ 1.  2.  3.]
[nan  0. nan]
[ 6.  7. nan]]
After removing nan values:
[1. 2. 3. 0. 6. 7.]
```

Pictorial Presentation: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Set comprehension:

```>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}
```