How can NaN values be removed from a NumPy array?

How can NaN values be removed from a NumPy array?

NaN (Not a Number) values can occur in a NumPy array when there is missing or invalid data. These values can affect the accuracy of calculations and analysis. To remove these NaN values, the NumPy library offers several methods. One approach is to use the np.isnan() function to identify the indices of the array that contain the NaN values. These indices can then be used to filter out the NaN values using the np.delete() function. Another method is to replace the NaN values with a specified value using the np.nan_to_num() function. Additionally, the np.where() function can be used to replace the NaN values with a specific value or interpolate them based on neighboring values. These methods allow for the removal of NaN values from a NumPy array, ensuring the accuracy of data analysis.

Remove NaN Values from NumPy Array (3 Methods)


You can use the following methods to remove NaN values from a NumPy array:

Method 1: Use isnan()

new_data = data[~np.isnan(data)]

Method 2: Use isfinite()

new_data = data[np.isfinite(data)]

Method 3: Use logical_not()

new_data = data[np.logical_not(np.isnan(data))]

Each of these methods produce the same result, but the first method is the shortest to type out so it tends to be used most often.

The following examples show how to use each method in practice.

Example 1: Remove NaN Values Using isnan()

The following code shows how to remove NaN values from a NumPy array by using the isnan() function:

import numpy as np

#create array of data
data = np.array([4, np.nan, 6, np.nan, 10, 11, 14, 19, 22])

#define new array of data with nan values removed
new_data = data[~np.isnan(data)]

#view new array
print(new_data)

[ 4.  6. 10. 11. 14. 19. 22.]

Notice that the two NaN values have been successfully removed from the NumPy array.

This method simply keeps all of the elements in the array that are not (~) NaN values.

Example 2: Remove NaN Values Using isfinite()

The following code shows how to remove NaN values from a NumPy array by using the isfinite() function:

import numpy as np

#create array of data
data = np.array([4, np.nan, 6, np.nan, 10, 11, 14, 19, 22])

#define new array of data with nan values removed
new_data = data[np.isfinite(data)]

#view new array
print(new_data)

[ 4.  6. 10. 11. 14. 19. 22.]

This method simply keeps all of the elements in the array that are finite values.

Since NaN values are not finite, they’re removed from the array.

Example 3: Remove NaN Values Using logical_not()

The following code shows how to remove NaN values from a NumPy array by using the logical_not() function:

import numpy as np

#create array of data
data = np.array([4, np.nan, 6, np.nan, 10, 11, 14, 19, 22])

#define new array of data with nan values removed
new_data = data[np.logical_not(np.isnan(data))]
#view new array
print(new_data)

[ 4.  6. 10. 11. 14. 19. 22.]

Notice that the two NaN values have been successfully removed from the NumPy array.

While this method is equivalent to the previous two, it requires more typing so it’s not used as often.

Additional Resources

The following tutorials explain how to perform other common operations in Python:

Cite this article

stats writer (2024). How can NaN values be removed from a NumPy array?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-nan-values-be-removed-from-a-numpy-array/

stats writer. "How can NaN values be removed from a NumPy array?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-nan-values-be-removed-from-a-numpy-array/.

stats writer. "How can NaN values be removed from a NumPy array?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-nan-values-be-removed-from-a-numpy-array/.

stats writer (2024) 'How can NaN values be removed from a NumPy array?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-nan-values-be-removed-from-a-numpy-array/.

[1] stats writer, "How can NaN values be removed from a NumPy array?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can NaN values be removed from a NumPy array?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top