For example, I have this array and calculate mean of rows:
a = np.array([[1,2,3],[2,np.NaN,4]])
mins = np.min(a, axis = 1)
The problem is the output is: [1. nan]
.
How to ignore nan in a and get result [1 2]
?
Another more concise and slightly faster alternative is to use the numpy.nanmin()
function, which does exactly what you asked for.
You can use masked arrays
mins = list(np.min(np.ma.masked_array(a, np.isnan(a)), axis=1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With