Curious2learn - 8 months ago 152

Python Question

I have a 2D numpy array. Some of the values in this array are

`NaN`

`[[ 0. 43. 67. 0. 38.]`

[ 100. 86. 96. 100. 94.]

[ 76. 79. 83. 89. 56.]

[ 88. NaN 67. 89. 81.]

[ 94. 79. 67. 89. 69.]

[ 88. 79. 58. 72. 63.]

[ 76. 79. 71. 67. 56.]

[ 71. 71. NaN 56. 100.]]

I am trying to take each row, one at a time, sort it in reversed order to get max 3 values from the row and take their average. The code I tried is:

`# nparr is a 2D numpy array`

for entry in nparr:

sortedentry = sorted(entry, reverse=True)

highest_3_values = sortedentry[:3]

avg_highest_3 = float(sum(highest_3_values)) / 3

This does not work for rows containing

`NaN`

`NaN`

Answer

This should work:

```
from numpy import *
a = array([[1, 2, 3], [0, 3, NaN]])
where_are_NaNs = isnan(a)
a[where_are_NaNs] = 0
```

In the above case where_are_NaNs is:

```
In [12]: where_are_NaNs
Out[12]:
array([[False, False, False],
[False, False, True]], dtype=bool)
```