I have the following array
[['AZ' 0.144] ['RZ' 14.021] ['BH' 1003.487] ['NE' 1191.514] ['FG' 550.991] ['MA' nan]]
Where Array dim is :
How would I return the 4 Row where values are the biggest ?
So the output would be :
[['RZ' 14.021] ['BH' 1003.487] ['NE' 1191.514] ['FG' 550.991]]
I tried :
But this does return an error :
TypeError: '<' not supported between instances of 'float' and 'str'
What am I doing wrong ?
You just sort by second column and get last 4 rows:
import numpy as np a = np.array( [['AZ', 0.144], ['RZ', 14.021], ['BH', 1003.487], ['NE', 1191.514], ['FG', 550.991], ['MA', np.nan]], ) a = a[~np.isnan(a[:, 1].astype(float))] srt = a[a[:, 1].astype(float).argsort()] print(srt[-4:, :])
Answered By – MSH
Answer Checked By – Willingham (BugsFixing Volunteer)