Home >Backend Development >Python Tutorial >How to Remove NaN (Not-a-Number) Values from a NumPy Array?
Removing NaN Values from a NumPy Array
NumPy arrays often contain missing or invalid data represented as NaN (Not-a-Number). Removing these values is essential for data manipulation or analysis. Here's how to accomplish this using NumPy:
Using Numpy.isnan and Array Indexing
To remove NaN values from an array x:
<code class="python">x = x[~numpy.isnan(x)]</code>
Explanation:
Example:
<code class="python">array = [1, 2, NaN, 4, NaN, 8] # Remove NaN values array_cleaned = array[~numpy.isnan(array)] print(array_cleaned) # Output: [1, 2, 4, 8]</code>
The above is the detailed content of How to Remove NaN (Not-a-Number) Values from a NumPy Array?. For more information, please follow other related articles on the PHP Chinese website!