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:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Creating a NumPy array 'x' containing integers and 'np.nan' (representing missing values)
x = np.array([200, 300, np.nan, np.nan, np.nan, 700])

# Creating a 2D NumPy array 'y' containing integers and 'np.nan' (representing missing values)
y = np.array([[1, 2, 3], [np.nan, 0, np.nan], [6, 7, np.nan]])

# Printing the original array 'x'
print("Original array:")
print(x)

# Removing 'np.nan' values from array 'x' and storing the result in 'result'
result = x[np.logical_not(np.isnan(x))]

# Printing the array 'x' after removing 'np.nan' values
print("After removing nan values:")
print(result)

# Printing a new line
print("\nOriginal array:")

# Printing the original array 'y'
print(y)

# Removing 'np.nan' values from array 'y' and storing the result in 'result'
result = y[np.logical_not(np.isnan(y))]

# Printing the array 'y' after removing 'np.nan' values
print("After removing nan values:")
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.]

Explanation:

The above code shows how to remove NaN (Not-a-Number) elements from two NumPy arrays: a 1D array 'x' and a 2D array 'y'.

x = np.array([200, 300, np.nan, np.nan, np.nan ,700]): Create a 1D NumPy array 'x' containing some numbers and NaN values.

y = np.array([[1, 2, 3], [np.nan, 0, np.nan] ,[6,7,np.nan]]): Create a 2D NumPy array 'y' containing some numbers and NaN values.

result = x[np.logical_not(np.isnan(x))]: Use np.isnan(x) to create a boolean array where 'True' represents a NaN value in 'x'. Then, use np.logical_not to invert this boolean array. Finally, use boolean indexing to extract elements from 'x' where the corresponding value in the inverted boolean array is 'True' (i.e., not NaN). Store the result in the variable 'result'.

result = y[np.logical_not(np.isnan(y))]: Repeat the same process for the 2D array 'y'. Note that 'y' is flattened before removing NaN values, so the resulting 'result' variable will be a 1D array.

Pictorial Presentation:

Python NumPy: Remove nan values from a given array

Python-Numpy Code Editor:

Previous: Write a NumPy program to convert a numpy array to an image. Display the image.
Next: Write a NumPy program to create a Cartesian product of two arrays into single array of 2D points.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/python-numpy-exercise-110.php