Skip to content
Justice Tech School

Handling NaN

NaN marks missing values. isnan finds them and nan functions ignore them.

Example
a = np.array([1.0, np.nan, 3.0])
print(a.sum(), np.nansum(a), np.nanmean(a))
print(np.isnan(a), np.nan_to_num(a))

Result

nan 4.0 2.0
[False  True False] [1. 0. 3.]

Exercise

Ignore NaN.

np.(a)

Quiz Test

What is NaN == NaN?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.