[SciPy-user] calculate average by excluding NaN value in Array

David Cournapeau david at ar.media.kyoto-u.ac.jp
Mon Mar 30 06:44:05 EDT 2009


David Cournapeau wrote:
> Ch B Komaki wrote:
>   
>> Dear Sirs/Madams
>> I wanna calculate average of some lists, but having Nan value makes
>> difficult to calculate average of data , I wanna exclude the position
>> has NaN.It should calculate  average of  the others instead  ;
>> for example;
>>
>>     
>
> nanmean should do what you want,
>
>   

which is in scipy.stats:

import numpy as np
from scipy.stats import nanmean

nanmean(np.array([1, 2, 3, np.nan])) # should return 2.

David



More information about the SciPy-User mailing list