Skip to content Skip to sidebar Skip to footer

How To Replace Nan Value With Zeros In A Numpy Array?

Say I have the next array a = np.array([1,2,3,Nan]) >>> a >>> [1 2 3 Nan] How can I replace the Nan with a zero? i.e. >>> [1 2 3 0]

Solution 1:

Edit 2020-10-07: please note that the duplicate answers contain some detailed information regarding nan replacement performance that it may be worth having a look at

A 5 sec web search gives the answer ...

https://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html

quoting :

numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)

Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.

Post a Comment for "How To Replace Nan Value With Zeros In A Numpy Array?"