. Advertisement .
..3..
. Advertisement .
..4..
Are you struggling to remove NaN from list in Python? Our guidelines will deliver three solutions to tackle this issue. Click to read the three methods detailed below.
How to Remove NaN from list in Python
Method 1: Use The Isnan()
What is isnan() exactly? It is a function popular in NumPy, assessing the NumPy array to see if the factor is NaN. The system will send back arrays containing boolean values as the output. Your yielded value will be “False” if the object isn’t NaN -and “True” if it’s NaN.
First of all, it is crucial to insert your NumPy library. Via the np.array() function, you may produce NumPy arrays featuring three NaN values and three integer values.
import numpy as np
from numpy import nan
example_arr = np.array([nan, 28, nan, 29, 19, 22])
The next step is to wrap this np.logical_not() function all-around your isnan()’s outputs. Why is that necessary? That way, the system can print non-NaN values into new arrays. Via logical_not(), all False values will transform into “True” (or vice versa).
In this case, non-NaN values are “True,” – and NaN values are “False.” Now, save this array into the “fixed_array” variables.
fixed_array = example_arr[np.logical_not(np.isnan(example_arr))]
print(fixed_array)
Output:
[28. 29. 19. 22.]
Let’s look at the whole code one more time:
import numpy as np
from numpy import nan
example_arr = np.array([nan, 28, nan, 29, 19, 22])
fixed_array = example_arr[np.logical_not(np.isnan(example_arr))]
print(fixed_array) # [28. 29. 19. 22.]
Method 2: Use The Filter()
It is a built-in asset that permits users to proceed with their iterable and single out items that fulfill given conditions. Such functions are practical for object extraction, particularly in number lists that do not serve as NaN values.
This example below shows you how to extract naN from the list with Filter().
Code:
from math import nan, isnan
nan_array= [1878, 1902, nan, 1899, nan, nan, 17, 6817, nan, nan, 29914]
fixed_nanArray = list(filter(lambda i: isnan(i) == False, nan_array))
print(fixed_nanArray)
Output:
[1878, 1902, 1899, 17, 6817, 29914]
Method 3: Use “Loop”
It is possible to extract NaN values from your list without built-in functions. Suppose we have defined and printed an “L1” list. The empty list “L2” also has its definition.
Now, utilize the “if” statement within the loop to assess whether items from “L1” are NaN. If it’s not NaN, this particular object will get appended to your “L2” list. Hence, the new list “L2” will get printed and generated.
from numpy import nan
array1 = ['Aries', 'Taurus', nan, nan, 'Scorpius', nan, 'Aquarius', 'Leo']
print(array1)
array2 =[]
for i in array1:
if str(i) != 'nan':
array2.append(i)
print(array2)
The output will display both array1 and array2:
['Aries', 'Taurus', nan, nan, 'Scorpius', nan, 'Aquarius', 'Leo']
['Aries']
['Aries', 'Taurus']
['Aries', 'Taurus', 'Scorpius']
['Aries', 'Taurus', 'Scorpius', 'Aquarius']
['Aries', 'Taurus', 'Scorpius', 'Aquarius', 'Leo']
This process might take some time on certain occasions; hence, it necessitates premature program quitting if your computer cannot accommodate all the data. Check out this guideline on how to do so.
Conclusion
This article has shown you how to remove NaN from list in Python. There are three methods to do so; choose one that you think is the easiest or suits you best! Feel free to leave comments or inquiries under the comment section, and ITtutoria will do our best to help you solve those issues.
Leave a comment