To remove NaN values from a NumPy array x:

x = x[~numpy.isnan(x)]
Explanation

The inner function numpy.isnan returns a boolean/logical array which has the value True everywhere that x is not-a-number. Since we want the opposite, we use the logical-not operator ~ to get an array with Trues everywhere that x is a valid number.

Lastly, we use this logical array to index into the original array x, in order to retrieve just the non-NaN values.

Answer from jmetz on Stack Overflow
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Remove NaN (np.nan) from an array | note.nkmk.me
January 23, 2024 - In NumPy, to remove rows or columns containing NaN (np.nan) from an array (ndarray), use np.isnan() to identify NaN and methods like any() or all() to extract rows or columns that do not contain NaN. ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-remove-nan-values-from-a-given-numpy-array
How to remove NaN values from a given NumPy array? - GeeksforGeeks
January 14, 2026 - import numpy as np arr = np.array([[12, 5, np.nan, 7], [2, 61, 1, np.nan], [np.nan, 1, np.nan, 5]]) res = arr[~np.isnan(arr)] print("2D array converted to 1D after removing NaNs ->", res)
Discussions

python - How to remove all rows in a numpy.ndarray that contain non-numeric values - Stack Overflow
I read in a dataset as a numpy.ndarray and some of the values are missing (either by just not being there, being NaN, or by being a string written "NA"). I want to clean out all rows cont... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I remove rows from a numpy array that have NaN as the first element? - Stack Overflow
How do I remove all rows from the array where nan is the first element? ... Use numpy.isnan to test the first element of of each row. More on stackoverflow.com
🌐 stackoverflow.com
python - Numpy: Drop rows with all nan or 0 values - Stack Overflow
I'd like to drop all values from a table if the rows = nan or 0. I know there's a way to do this using pandas i.e pandas.dropna(how = 'all') but I'd like a numpy method to remove rows with all nan... More on stackoverflow.com
🌐 stackoverflow.com
February 26, 2014
How should I initialize a numpy array of NaN values?
>>> np.full(3, np.nan) But the bigger question is why would you want to? Edit: as an explanation, your example does not work because you initialized an array of ints. ints have no "NaN" value, only floats do. So your method would work if you initialized an array of floats: >>> x = np.array([0.0,0.0,0.0]) >>> x.fill(np.nan) >>> x array([ nan, nan, nan]) Or converted the ints to floats: >>> x = np.array([0,0,0], dtype=np.float) >>> x.fill(np.nan) >>> x array([ nan, nan, nan]) But the np.full() method is much better. More on reddit.com
🌐 r/learnpython
5
5
April 8, 2016
🌐
Skytowner
skytowner.com › explore › removing_rows_containing_nan_in_a_numpy_array
Removing rows containing NaN in a NumPy array
To remove rows containing NaN in a NumPy array, we can use a combination of the isnan(~) and any(~) methods.
🌐
Statology
statology.org › home › how to remove nan values from numpy array (3 methods)
How to Remove NaN Values from NumPy Array (3 Methods)
May 27, 2022 - Since NaN values are not finite, they’re removed from the array. The following code shows how to remove NaN values from a NumPy array by using the logical_not() function:
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-91.php
NumPy: Remove all rows in a NumPy array that contain non-numeric values - w3resource
August 29, 2025 - Write a NumPy program to remove all rows in a NumPy array that contain non-numeric values. ... # Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing various data types including integers, NaN (Not a Number), and booleans x = np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [True, False, True]]) # Printing a message indicating the original array will be displayed print("Original array:") # Printing the original array 'x' with its elements print(x) # Printing a message indicating the removal of all non-numeric elements from the array prin
🌐
Medium
medium.com › @keycomputereducation › numhow-to-remove-nan-value-from-numpy-array-in-python-736939c5bd14
NumHow to remove nan value from Numpy array in Python? | by Key computer Education | Medium
May 1, 2022 - import mathprint (math.isnan (56))print (math.isnan (-45))print (math.isnan (+45.34))print (math.isnan (math.inf))print (math.isnan (float(“nan”)))print (math.isnan (float(“inf”)))print (math.isnan (float(“-inf”)))print (math.isnan (math.nan)) ... import mathimport numpy as npx1 = np.array([1, 2, 3, np.nan, np.nan, 4, 5, 6, np.nan, 7, 8, 9, np.nan])x2 = np.array([np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])x3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])b1 = [not math.isnan(number) for number in x1]b2 = [not math.isnan(number) for number in x2]b3 = [not math.isnan(number) for number in x3]print(b1)print(b2)print(b3)
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-110.php
Python NumPy: Remove nan values from a given array - w3resource
August 29, 2025 - # 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 removin
🌐
Delft Stack
delftstack.com › home › howto › numpy › numpy remove nan values
How to Remove Nan Values From a NumPy Array | Delft Stack
February 2, 2024 - Using the isnan() function, we can create a boolean array that has False for all the non nan values and True for all the nan values. Next, using the logical_not() function, We can convert True to False and vice versa.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-remove-rows-in-numpy-array-that-contains-non-numeric-values
How to Remove Rows in Numpy Array that Contains Non-Numeric Values? - GeeksforGeeks
December 13, 2025 - This method filters rows by checking if they contain any NaN. Rows without NaN are kept; rows with even one NaN are removed. ... import numpy as np a = np.array([[10.5, 22.5, 3.8], [41, np.nan, np.nan]]) clean = a[~np.isnan(a).any(axis=1)] print(clean)
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_removing_missing_data.htm
NumPy - Removing Missing Data
In this example, we use np.isnan() function combined with any() function to create a mask that identifies rows containing NaN values. We then use this mask to filter out and remove those rows from the original 2D array − · import numpy as ...
🌐
ProjectPro
projectpro.io › recipes › drop-all-missing-values-from-numpy-array
How to remove nan values from numpy array - Projectpro
December 23, 2022 - Droping the missing values or nan values can be done by using the function "numpy.isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy.logical_not()" where the boolean values ...
🌐
Medium
medium.com › @ryan_forrester_ › remove-nan-from-lists-in-python-a-complete-guide-d6656077f664
Remove NaN from Lists in Python: A Complete Guide | by ryan | Medium
January 7, 2025 - 2. `math.isnan(x)` checks if the float is NaN — This only works on float values — That’s why we needed the isinstance check first · 3. `x is not None` checks for None values — We use `is` instead of `==` because None is a special Python object — This is the proper way to check for None · When you’re working with larger datasets, NumPy makes life much easier. Here’s how to use it: import numpy as np # Create a NumPy array with NaN values arr = np.array([1, np.nan, 3, np.nan, 5, None]) # Method 1: Using np.isnan (fastest method) cleaned = arr[~np.isnan(arr)] print(cleaned) # [1.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to remove nan values from numpy arrays
5 Best Ways to Remove NaN Values from NumPy Arrays - Be on the Right Side of Change
February 20, 2024 - The tilde (~) operator is used to invert this mask, and the resultant boolean array is used to index and filter out the NaN values. The numpy.compress function can be combined with numpy.isnan to remove NaN values from an array.
🌐
IncludeHelp
includehelp.com › python › how-to-remove-nan-values-from-a-given-numpy-array.aspx
How to remove NaN values from a given NumPy array?
Suppose we need to create a NumPy array of length n, and each element of this array would be e a single value (say 5). To remove NaN values from a given NumPy array, you can use a method provided by NumPy called the full() method.
🌐
py4u
py4u.org › blog › how-to-remove-nan-values-from-a-given-numpy-array
How to Remove NaN Values from a Given NumPy Array?
Vectorize Operations: Use NumPy’s vectorized functions (e.g., np.isnan(), boolean indexing) to avoid slow Python loops. Context Matters: Decide if removing NaNs is appropriate (e.g., missing data might require imputation—e.g., mean/median—instead of removal). Handle 2D Arrays Carefully: When removing rows/columns, verify the array’s shape to avoid unexpected data loss (e.g., removing rows with all NaNs is safer than any NaN in some cases).