Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the mean value of list containing NaNs in Python

Tags:

python

numpy

I would like to know how to get the mean value of a list which contains some NaNs. By using:

np.mean(mylist)

the result as expected gives NaN which is wrong. What should i do to get the mean?

like image 627
azal Avatar asked Dec 11 '14 11:12

azal


1 Answers

Use np.nanmean to ignore the NaNs:

np.nanmean(mylist)

For example,

In [108]: np.nanmean([np.nan, 1, 2, np.nan, 3])
Out[108]: 2.0
like image 125
unutbu Avatar answered Sep 18 '22 12:09

unutbu