group the rows having same first element and find max according to last column.
Like row 0 and 1 have same starting element so go to last column and find max from it and return that row without looping .
utl = np.array ([[ 21. , 0.01 ],
[ 21. , 0.02 ],
[ 26. , 0.04 ],
[ 26. , 0.03 ],
[ 26. , 0.03 ],
[ 34. , 0.03 ],
[ 34. , 0.09 ],
[ 26. , 0.03 ]])
output must be
[ 21. , 0.02 ]
[ 34. , 0.09 ]
[ 26. , 0.04 ]
CodePudding user response:
If the final order of the rows is not important:
# sort by first and second column
a = utl[np.lexsort((utl[:,1], utl[:,0]))]
# get positions of group change
# as we want the max, we take the last row per group
_, idx = np.unique(a[:,0], return_index=True)
idx2 = (idx-1)%a.shape[0] # or idx2 = np.r_[idx[1:]-1, [a.shape[0]-1]]
# split
a[idx2]
output:
array([[21, 0.02],
[26, 0.04],
[34, 0.09]])
solution for the min
a = utl[np.lexsort((utl[:,1], utl[:,0]))]
_, idx = np.unique(a[:,0], return_index=True)
a[idx]
CodePudding user response:
If you use np.lexsort, you can get the maximum by index:
sort_idx = np.lexsort(utl.T[::-1])
The differences in the sorted first column are going to tell you which index to grab from the second:
max_idx = np.r_[np.flatnonzero(np.diff(utl[idx, 0])), len(idx) - 1]
min_idx = np.r_[0, np.flatnonzero(np.diff(utl[idx, 0])) 1]
The results can be extracted immediately:
minima = utl[idx[min_idx], 1]
maxima = utl[idx[max_idx], 1]
