I am writing a program to sort the names of amino acids depending on their energy value within a particular company.
I have extracted relevant data to the following numpy array.
And I tried this
In[37]: Data = np.array([
['ASN 205', -9.64164],
['LEU 206', -8.985774],
['ASN 207', -7.314434],
['PRO 208', -4.105338],
['ASN 209', -2.092342],
['GLY 210', -2.101412],
['LYS 211', -2.483852],
['ARG 212', -24.20364],
['SER 213', -1.181002],
['VAL 214', 0.057618]])
In[38]: ind3 = np.lexsort((Data[:,0],Data[:,1]))
In[39]: Result = Data[ind3]
In[40]: Result
Out[40]:
array([['SER 213', '-1.181002'],
['ASN 209', '-2.092342'],
['GLY 210', '-2.101412'],
['LYS 211', '-2.483852'],
['ARG 212', '-24.20364'],
['PRO 208', '-4.105338'],
['ASN 207', '-7.314434'],
['LEU 206', '-8.985774'],
['ASN 205', '-9.64164'],
['VAL 214', '0.057618']],
dtype='|S9')
But the problem here is float values are arranged in a lexicographic way. I want it be be ordered according to their value means -24.20364 first then ...-2.483852.
How do I do this?
numpy.sortsays this :"order : list, optional When a is a structured array, this argument specifies which fields to compare first, second, and so on."Dataarray has been immediately converted to all-string-array (dtype='|S9'). String is not a good format for floats. Think of a better data structure, such as a dict.TransposedData = numpy.transpose(Data)Result = Data[:,np.argsort(Data[1].astype(float))]Done