In this article, we are going to learn how to remove Nan values from a given array. Nan values are those values that do not have a specific value associated with them or they are different from the type of values that are to be used in the declared array.
There are basically three approaches with slight differences in syntax. Either we could use a function specified in NumPy or we could use an operator, the basic working will be the same.
The numpy.isnan() will give true indexes for all the indexes where the value is nan and when combined with numpy.logical_not() function the boolean values will be reversed. So, in the end, we get indexes for all the elements which are not nan. From the indexes, we can filter out the values that are not nan and save them in another array.
Python3
import numpy
a = numpy.array([ 5 , 2 , 8 , 9 , 3 , numpy.nan,
2 , 6 , 1 , numpy.nan])
b = a[numpy.logical_not(numpy.isnan(a))]
print ( "original 1D array ->" , a)
print ( "1D array after removing nan values ->" , b)
print ()
c = numpy.array([[ 6 , 2 , numpy.nan], [ 2 , 6 , 1 ],
[numpy.nan, 1 , numpy.nan]])
d = c[numpy.logical_not(numpy.isnan(c))]
print ( "Original 2D array ->" )
print (c)
print ( "2D array converted to 1D after removing nan values " , d)
|
Output:
Note: No matter what the Dimension of the array is, it will be flattened into a 1D array
Using np.isnan() Remove NaN values from a given NumPy
Combining the ~ operator instead of numpy.logical_not() with numpy.isnan() function. This will work the same way as the above, it will convert any dimension array into a 1D array.
Python3
import numpy
c = numpy.array([[ 12 , 5 , numpy.nan, 7 ],
[ 2 , 61 , 1 , numpy.nan],
[numpy.nan, 1 ,
numpy.nan, 5 ]])
d = c[~(numpy.isnan(c))]
print ( "Original 2D array " )
print (c)
print ()
print ( "2D array converted to 1D after removing nan values " )
print (d)
|
Output:
Using np.isfinite Remove NaN values from a given NumPy
The numpy.isfinite() function tests element-wise whether it is finite or not(not infinity or not Not a Number) and returns the result as a boolean array. Using this function we will get indexes for all the elements which are not nan. From the indexes, we can filter out the values that are not nan and save them in another array.
Python3
import numpy as np
c = np.array([[ 12 , 5 , np.nan, 7 ],
[ 2 , 61 , 1 , np.nan],
[np.nan, 1 ,
np.nan, 5 ]])
d = c[np.isfinite(c)]
print ( "Original 2D array " )
print (c, '\n' )
print ( "2D array converted to 1D after removing nan values " )
print (d)
|
Output:
Original 2D array
[[12. 5. nan 7.]
[ 2. 61. 1. nan]
[nan 1. nan 5.]]
2D array converted to 1D after removing nan values
[12. 5. 7. 2. 61. 1. 1. 5.]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Sep, 2022
Like Article
Save Article