hi all, how can I replace the NaN value in an array, zero if an operation is performed such that as a result instead of the NaN value is zero operations as
0 / 0 = NaN can be replaced by 0
hi all, how can I replace the NaN value in an array, zero if an operation is performed such that as a result instead of the NaN value is zero operations as
0 / 0 = NaN can be replaced by 0
If you have Python 2.6 you have the math.isnan() function to find NaN values.
With this we can use a list comprehension to replace the NaN values in a list as follows:
import math
mylist = [0 if math.isnan(x) else x for x in mylist]
If you have Python 2.5 we can use the NaN != NaN trick from this question so you do this:
mylist = [0 if x != x else x for x in mylist]