Issue
Why can't I detect the np.nan value in data using np.isnan() in the list comprehension below? Does the list comprehension transform the type of values in some way?
data = pd.DataFrame({'col':['a', 'b', np.nan]})
[print('NaN') if np.isnan(i) else print('Not NaN') for i in data.col]
Solution
Yes, you will get into trouble using np.isnan()
because of the mixed types in the column. From pandas' docs
Because NaN is a float, a column of integers with even one missing values is cast to floating-point dtype (see Support for integer NA for more)
Therefore you should consider, as @saeedghadiri suggested using pd.isna()
:
[print('NaN') if pd.isna(i) else print('Not NaN') for i in data.col]
Answered By - Celius Stingher
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.