iterating through two numpy arrays applying a function in Python -
i have
import numpy np = np.array([np.nan,2,3]) b = np.array([1,np.nan,2])
i want apply function a,b, there fast way of doing this. (like in pandas, can apply)
specifically interesting in averaging , b, take average 1 of numbers when other number missing.
i.e. want return
np.array([1,2,2.5])
for example above. however, know answer in more general setting (where want apply operation element-wise number of numpy arrays)
you can use numpy.nanmean
, ignores nan
s:
np.nanmean([a, b], axis=0) # array([ 1. , 2. , 2.5])
wiki
Comments
Post a Comment