How to Remove NaN or Missing Values From a NumPy Array in Python?

Remove NaN from numpy array

Are you looking for a solution on how to remove NaN or missing values from NumPy array? Then keep reading; this guide will provide you with a variety of solutions 😉

In Python, NaN stands for Not a Number. In this article, we’ll use different approaches to remove NaN from the NumPy arrays. In data analysis, NaN values are considered one of the significant issues. To get the desired outcome it is essential to Remove NaN values from a NumPy Arrays. Dealing with NaN values in arrays, data frames, and series is easy as compared to dealing with them alone. 

That being said, in this article we’ll provide two solutions to remove NaN from NumPy array:

  1. Using the numpy.isnan() function
  2. Using the numpy.isfinite() function

For more information and help, check out our other similar guides to troubleshooting NumPy related problems and installing NumPy correctly:

 

 

2 Methods to Remove NaN Values From NumPy Array

Method 1: Using numpy.isnan()

The numpy.isnan() function will provide those true indexes where we will only be left with the NaN values and when this function is integrated with numpy.logical_not() it will help the boolean values to be reversed. In the end, we’ll get a NumPy array without NaN values. By doing this, we can remove NaN values from a NumPy array.

 

Remove NaN Values From 1-D NumPy Array Using np.isnan() Function

In the below example we have created a 1-D NumPy array. By using the np.isnan() function we’ll remove NaN values from it.

 

Code

# import numpy

import numpy

 

# 1-D numpy array created

Array1 = numpy.array([7, 1, 8, 2, 4, numpy.nan,

                 5, 9, 6, numpy.nan])

 

# NaN values are removed using numpy.logical_not and numpy.isnan()

Array2 = Array1[numpy.logical_not(numpy.isnan(Array1))]



# displaying results

print("All the values including NaN:      ", Array1)

print("Without NaN values:  ", Array2)

 

Output

All the values including NaN:             [ 7.  1.  8.  2.  4. nan  5.  9.  6. nan] 
Without NaN values:    [7. 1. 8. 2. 4. 5. 9. 6.]

 

Remove NaN Values From 2-D NumPy Array Using np.isnan() Function

In the below example we have created a 2-D NumPy array. By using the np.isnan() function we’ll remove NaN values from it.  After removing NaN from a 2-D NumPy we’ll convert the array into a 1-D array. No matter what the dimension of the NumPy array is, it will be converted into a 1-D array.

 

Code

# import numpy

import numpy

 

# 2D numpy array has been created

Array1 = numpy.array([[9, 1, numpy.nan], [4, 7, 3],

                 [numpy.nan, 2, numpy.nan]])

 

# nan values are removed using numpy.logical_not and numpy.isnan()

Array2 = Array1[numpy.logical_not(numpy.isnan(Array1))]

 

# displaying results

print("2-D numpy array with NaN values: " )

print(Array1)

print("Without NaN values:   ", Array2)

 

 

Output

2-D numpy array with NaN values: 

[[ 9.  1. nan]

 [ 4.  7.  3.]

 [nan  2. nan]]

Without NaN values: 

[9. 1. 4. 7. 3. 2.]

 

Here we have combined the numpy.isnan() function with the (~) operator. It will help us in removing NaN values from a NumPy array. Let’s understand it further using the below example:

 

Code

# import numpy

import numpy

 

# 2-D array has been created

Array1 = numpy.array([[14, 6, numpy.nan, 8],

[3, 59, 2, numpy.nan],

[numpy.nan, 2,

numpy.nan, 6]])

 

# nan values are removed using numpy.logical_not and numpy.isnan()

Array2 = Array1[~(numpy.isnan(Array1))]

 

# displaying results

print("2-D numpy array with NaN values: ")

print(Array1)

print()

 

print("Without NaN values: ")

print(Array2)

 

Output

2-D numpy array with NaN values: 

[[14.  6. nan  8.]

 [ 3. 59.  2. nan]

 [nan  2. nan  6.]]

 

Without NaN values: 

[14.  6.  8.  3. 59.  2.  2.  6.]

 

 

Method 2: Using np.isfinite()

The numpy.isfinite() function helps in testing the flow element-wise and checks whether the elements are infinite or not and it will return an array as a boolean. Furthermore, using the numpy.isfinite() function we will get an array without NaN values. So, we can say one can find this function helpful if they want to remove NaN values from a NumPy array.

 

Code

# import numpy

import numpy as np

 

# 2D numpy array has been created

Array1 = np.array([[13, 6, np.nan, 8],

[3, 64, 2, np.nan],

[np.nan, 2,

np.nan, 6]])

 

# nan values are removed using np.isfinite

Array2 = Array1[np.isfinite(Array1)]

 

# displaying results

print("2-D numpy array with NaN values: ")

print(Array1, '\n')




print("Without NaN values:  ")

print(Array2)


 

Output

2-D numpy array with NaN values: 

[[13.  6. nan  8.]

 [ 3. 64.  2. nan]

 [nan  2. nan  6.]] 




Without NaN values:  

[13.  6.  8.  3. 64.  2.  2.  6.]

 

 

Conclusion

To conclude the article on how to remove NaN from a NumPy array we’ve discussed the different approaches including numpy.isnan() function and np.isfinite() function. These two functions help us in removing NaN values from a NumPy array. It is a good idea to remove NaN from an array when one can not easily identify it.

Here is a quick recap to the topics we have discussed in the article

  1. What does NaN mean?
  2. How to remove NaNs from a NumPy array using numpy.isnan()?
  3. How to remove NaNs from NumPy arrays using numpy.isfinite()?

Hope you find this article helpful 😉 Do let us know in the comment section which approach helps you in removing NaN values from a NumPy array 🥰

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share