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 - Applying the negation operator (~) to this resulting ndarray turns NaN to False, which can be used as a mask to remove NaN (extract non-NaN values).
🌐
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 - NumPy provides efficient methods to identify and filter out missing values, ensuring clean and reliable datasets. ... Output: [5. 8. 2. 6. 1. 3.] The ~ operator reverses the boolean array returned by np.isnan(), keeping only the non-NaN elements.
🌐
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 - The following code shows how to remove NaN values from a NumPy array by using the isnan() function:
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-remove-nan-values-from-a-given-numpy-array
How to remove NaN values from a given NumPy array? - GeeksforGeeks
September 7, 2022 - Many times NumPy arrays may contain NaN values that need to be removed to ensure the array is free from unnecessary or invalid data. This can be achieved using the np.isnan() function along with the Bitwise NOT operator.
🌐
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 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])print(x1[np.isfinite(x1)])print(x2[np.isfinite(x2)])print(x3[np.isfinite(x3)])
🌐
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
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan() | note.nkmk.me
January 23, 2024 - You can use np.nanmean() to replace NaN with the mean of non-NaN values. This replacement can be done for the entire array or separately for each row or column. NumPy: Functions ignoring NaN (np.nansum, np.nanmean, etc.)
🌐
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 - The boolean array will store False for all the nan values and True for all the finite values. We will use this function to retrieve a boolean array for the target array. Using boolean indexing, We will filter all the finite values. Again, as mentioned above, indexes with True values will be used to filter the array. Here’s the example 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)
Find elsewhere
🌐
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 ...
🌐
CodeGym
codegym.cc › java blog › learning python › remove nan using python
Remove NaN Using Python
August 14, 2024 - We convert the numpy array to a pandas Series. Use dropna() to remove the NaN values.
🌐
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.
🌐
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 - Sometimes you’ll have lists inside lists, and removing NaN values gets trickier. Here’s a robust solution: import math import numpy as np def clean_nested_list(nested_data): """Remove NaN values from lists within lists""" # If we're looking at a list, process each item in it if isinstance(nested_data, list): # Process each item, but only keep non-NaN values return [ clean_nested_list(item) for item in nested_data if not (isinstance(item, float) and math.isnan(item)) and item is not None ] # If it's not a list, return it unchanged return nested_data # Test with a complex nested structure nested_data = [ [1, float('nan'), [3, None]], [math.nan, 5, [6, float('nan')]], [7, [8, None, 9]] ] cleaned = clean_nested_list(nested_data) print("Cleaned nested data:") print(cleaned) # Output: [[1, [3]], [5, [6]], [7, [8, 9]]]
🌐
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 - After initializing a NumPy array with NaN values, this snippet creates a boolean mask using np.isnan which is then inverted with the tilde (~) operator. The np.compress function takes this mask and the original array to return a new array with NaN values removed.
🌐
IncludeHelp
includehelp.com › python › how-can-i-remove-nan-from-list-numpy-array.aspx
Python - How can I remove Nan from list NumPy array?
October 8, 2023 - For this purpose, we will create a list of the elements of an array where we will select only those elements which are not nan. ... # Import numpy import numpy as np # Creating a numpy array arr = np.array([2,np.nan,4,3,4,np.nan,7,4,7,np.nan,4,3]) # Display original array print("Original Array:\n",arr,"\n") # Determining whether arr is numeric or not res = [ x for x in arr if str(x) != 'nan'] # Display result print("Result is:\n",res,"\n")
🌐
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)