[Solutions] Identify the first & last non-zero elements/indices within a group in numpy
========= [Previous question]. Please ignore the followings =========
I have numpy arrays like the following:
group = np.array([1,1,1,1,1,1,1,2,2,2,2,2,2,2])
arr1 = np.array([0,0,0,np.nan,2,np.nan,np.nan,0,np.nan,2,np.nan,np.nan,0,0])
arr2 = np.array([0,0,0,np.nan,np.nan,3,np.nan,0,np.nan,2,np.nan,np.nan,0,0])
target_arr = np.array([0,0,0,0,2,2,2,0,0,2,2,2,0,0])
For group 1, the first non-zero/nan element is 2 in arr1 at index 4. For group 2, it is 2 in arr1 and arr2 at index 2. How do I identify the first-appearing & the first-non-zero/nan value for each group in multiple arrays (i.e. only one value for each group), and ffill the values to create one array (like target_arr aobve) without iteration?
I found a similar answer by using pandas. How do I do it in pure numpy? Identify first non-zero element within a group in pandas