How To Remove Nan (float) Item(s) From A List Of Mixed Data Types In Python
I have a list with mixed data types: Z = ['a','b', float('NaN'), 'd'] I would like to remove the nan observations. I tried the following based on a couple of multiple suggestions
Solution 1:
A nice hack is do Z = [x for x in Z if x == x]
This works since NaN is not equal to itself.
Solution 2:
you can use numpy
import numpy as np
arr = np.array([0, 1, 2, 4, np.nan, 8, 3, np.nan, 6])
print(arr)
>>>[ 0. 1. 2. 4. nan 8. 3. nan 6.]
arr = arr[np.where(~np.isnan(arr))]
print(arr)
>>>[0. 1. 2. 4. 8. 3. 6.]
Post a Comment for "How To Remove Nan (float) Item(s) From A List Of Mixed Data Types In Python"