Use is.nan() function with an if not statement in Python to find the first non-nan value in a list.
The is.nan() function helps us detect NaN values and then negate them using an if-not statement, filtering out all NaN values and returning only the values we need, which is the first non-NaN value we are looking for.
You can represent the NaN value using float(‘nan’) or np.nan (If you are using the Numpy library.)
import math def first_non_nan(main_list): for element in main_list: if not math.isnan(element): return element return None main_list = [float('nan'), float('nan'), 21.0, 42.0, float('nan')] first_non_nan = first_non_nan(main_list) print(first_non_nan) # Output: 21.0
In the above code, our input list contains a total of three NaNs. The first two NaNs are at positions 0 and 1.
Then, a non-nan element appears, which is 21.0. So, logically, the answer should be 21.0 since it is the first non-nan element. As soon as it finds the non-nan element, it breaks the loop and returns that first value.
If there are no non-NaN values (list with all the NaNs), it will return None because there is no other value other than NaN.
If you have a list with simple float values, you can use this approach.
Empty list
What if the input list is empty? How will you deal with it? Well, based on our code, it will return None because there is no non-NaN value.
import math def first_non_nan(empty_list): for element in empty_list: if not math.isnan(element): return element return None empty_list = [] first_non_nan = first_non_nan(empty_list) print(first_non_nan) # Output: None
List with All NaN Values
What if the list contains all the NaN values and not a single non-NaN value? Well, it will still return None.
import math def first_non_nan(nan_list): for element in nan_list: if not math.isnan(element): return element return None nan_list = [float('nan'), float('nan'), float('nan')] first_non_nan = first_non_nan(nan_list) print(first_non_nan) # Output: None
Using NumPy (with np.isnan)
If your input data is a numpy array, you can use numpy.isnan() method to create a boolean array where True indicates NaN values.
Using ~np.isnan(), we can invert the mask to select non-NaN values.
Use array indexing to find the first element and then use the .any() method to check if there’s at least one non-NaN value to avoid index errors.
import numpy as np def first_non_nan_numpy(np_array): mask = ~np.isnan(np_array) return np_array[mask][0] if mask.any() else None np_array = np.array([np.nan, np.nan, 21.0, 42.0, np.nan, 84.0]) first_non_np_nan = first_non_nan_numpy(np_array) print(first_non_np_nan) # Output: 21.0
And we get the exact output, but this time it is from the array. Use NumPy for large numerical datasets or when working with arrays.
For mixed types, use type checking or try-except blocks to handle exceptions.