In this article, we are going to learn how to remove Nan values from a given array. man values are those values that do not have a specific value associated with it or they are different from the type of values that are to be used in the declared array.
There are basically two approaches that work in the same way, just a slight difference in syntax. Either we could use a function specified in NumPy or we could use an operator, the basic working will be the same.
Method #1 : Using numpy.logical_not() and numpy.nan() functions
The numpy.isnan() will give true indexes for all the indexes where the value is nan and when combined with numpy.logical_not() function the boolean values will be reversed. So, in the end, we get indexes for all the elements which are not nan. From the indexes, we can filter out the values that are not nan and save it in another array.
Note: No matter what Dimension of the array is, it will be flattened into a 1D array
Method #2 : Combining the ~ operator instead of numpy.logical_not() with numpy.isnan() function. This will work the same way as the above, it will convert any dimension array into 1D array.
In the below code only the 2D array is shown for example.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Visualize missing values (NaN) values using Missingno Library
- Python | Replace NaN values with average of columns
- How to Drop Rows with NaN Values in Pandas DataFrame?
- Ways to Create NaN Values in Pandas DataFrame
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Replace NaN Values with Zeros in Pandas DataFrame
- Count NaN or missing values in Pandas DataFrame
- How to count the number of NaN values in Pandas?
- Replace all the NaN values with Zero's in a column of a Pandas dataframe
- Count the NaN values in one or more columns in Pandas DataFrame
- Highlight the nan values in Pandas Dataframe
- How to Drop Columns with NaN Values in Pandas DataFrame?
- How to Remove rows in Numpy array that contains non-numeric values?
- How to Remove columns in Numpy array that contains non-numeric values?
- Python - Remove duplicate values across Dictionary Values
- Python - Remove keys with Values Greater than K ( Including mixed values )
- Python | cmath.nan Constant
- Check for NaN in Pandas DataFrame
- Calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis
- Remove infinite values from a given Pandas DataFrame
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.