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
๐ŸŒ
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) ...
๐ŸŒ
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. ...
๐ŸŒ
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:
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ how do i remove nan values from a numpy array?
How do I remove NaN values from a NumPy array? | Codemia
September 23, 2025 - NumPy, the foundational library for numerical computing, uses NaN as a floating-point sentinel value defined by the IEEE 754 standard. Knowing how to detect, remove, and work around NaN values is essential for producing correct analytical results. Before removing NaN values you need to find them. The function np.isnan() returns a boolean array of the same shape as the input, with True wherever a NaN appears.
๐ŸŒ
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 - These two ways involve isnan() function from math library and isnull function from pandas library. Both these functions check whether an element is nan or not and return a boolean result.
Find elsewhere
๐ŸŒ
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 - These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np.isnan() function.
๐ŸŒ
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)
๐ŸŒ
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 - It then uses np.isnan to create a boolean mask where True corresponds to NaN values. 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.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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 - # 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")