Remove Nan values from NumPy array
This article discusses some built-in NumPy functions that you can use to remove nan
values.
Remove Nan values using logical_not()
and methods in NumPyisnan()
logical_not() is used to apply logical NOT to the elements of an array. isnan()
It is a Boolean function that checks whether an element is nan.
Using isnan()
the function, we can create a Boolean array that nan
has all non- values of False
and all nan
values of True
. Next, using logical_not()
the function, we can True
convert from to False
and vice versa.
Finally, using boolean indexing, we can filter all non- values from the original NumPy array nan
. All True
indices with as their value will be used to filter the NumPy array.
For an in-depth look at these functions, please refer to their official documentation, here and here, respectively.
Refer to the following code snippet for the solution.
import numpy as np
myArray = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
output1 = myArray[np.logical_not(np.isnan(myArray))] # Line 1
output2 = myArray[~np.isnan(myArray)] # Line 2
print(myArray)
print(output1)
print(output2)
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
Line 2
is Line 1
a simplified version of .
isfinite()
Remove Nan values using method in NumPy
As the name suggests, isfinite()
the function is a boolean function that checks if an element is a finite value. It can also check for finite values in an array and return a boolean array for that array. The boolean array will nan
store for all values False
, for all finite values True
.
We will use this function to retrieve a boolean array for the target array. Using boolean indexing, we will filter all finite values. Again, as mentioned above, True
the index with the value will be used to filter the array.
Here is the sample code.
import numpy as np
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
output1 = myArray1[np.isfinite(myArray1)]
output2 = myArray2[np.isfinite(myArray2)]
output3 = myArray3[np.isfinite(myArray3)]
print(myArray1)
print(myArray2)
print(myArray3)
print(output1)
print(output2)
print(output3)
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
To learn more about this function, refer to the official documentation
Use math.isnan
the method to remove Nan values
In addition to these two NumPy solutions, there are two other nan
ways to remove a value. These two ways involve the function math
in the library isnan()
and the function pandas
in the library isnull
. Both functions check whether an element is nan
, and return a Boolean result.
Here is isnan()
a solution using the method.
import numpy as np
import math
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
booleanArray1 = [not math.isnan(number) for number in myArray1]
booleanArray2 = [not math.isnan(number) for number in myArray2]
booleanArray3 = [not math.isnan(number) for number in myArray3]
print(myArray1)
print(myArray2)
print(myArray3)
print(myArray1[booleanArray1])
print(myArray2[booleanArray2])
print(myArray3[booleanArray3])
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
Use pandas.isnull
the method to remove Nan values
Here is a solution using the method pandas
from .isnull()
import numpy as np
import pandas as pd
myArray1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])
myArray2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
myArray3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
booleanArray1 = [not pd.isnull(number) for number in myArray1]
booleanArray2 = [not pd.isnull(number) for number in myArray2]
booleanArray3 = [not pd.isnull(number) for number in myArray3]
print(myArray1)
print(myArray2)
print(myArray3)
print(myArray1[booleanArray1])
print(myArray2[booleanArray2])
print(myArray3[booleanArray3])
print(myArray1[~pd.isnull(myArray1)]) # Line 1
print(myArray2[~pd.isnull(myArray2)]) # Line 2
print(myArray3[~pd.isnull(myArray3)]) # Line 3
Output:
[ 1. 2. 3. nan nan 4. 5. 6. nan 7. 8. 9. nan]
[nan nan nan nan nan nan]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[]
[ 1 2 3 4 5 6 7 8 9 10]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Converting a PIL image to a NumPy array
Publish Date:2025/05/03 Views:148 Category:Python
-
This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:162 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to get value from Pandas DataFrame cell
Publish Date:2025/05/02 Views:148 Category:Python
-
We'll look at using to get values from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .
How to Add a Row to a Pandas DataFrame
Publish Date:2025/05/02 Views:127 Category:Python
-
Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to