Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate min of rows ignoring NaN values

Tags:

python

numpy

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]?

like image 826
user3654650 Avatar asked Nov 27 '14 23:11

user3654650


2 Answers

Another more concise and slightly faster alternative is to use the numpy.nanmin() function, which does exactly what you asked for.

like image 50
Sven Marnach Avatar answered Nov 05 '22 20:11

Sven Marnach


You can use masked arrays

mins = list(np.min(np.ma.masked_array(a, np.isnan(a)), axis=1))
like image 27
lejlot Avatar answered Nov 05 '22 20:11

lejlot