I have the data of the form:
#---------------------
# Data
#---------------------
p q r
2 8 14
2 9 22
1 5 19
2 7 19
3 11 13
2 7 20
1 4 15
3 12 17
1 4 14
1 5 20
2 7 17
3 10 13
3 11 20
3 11 14
1 6 18
3 12 16
2 9 21
3 10 19
2 8 13
1 6 22
1 4 13
2 8 15
3 12 15
3 10 16
2 9 16
1 5 16
1 6 21
Now I need to sort this data using NumPy in the following manner:
- Ascending order for column p.
- Ascending order for column q.
- Descending order for column r.
I used the following code, but it does not sort correctly:
import numpy as np
data = open('data.dat', "r")
line = data.readline()
while line.startswith('#'):
line = data.readline()
data_header = line.split("\t")
data_header[-1] = data_header[-1].strip()
_data_ = np.genfromtxt(data, comments='#', delimiter='\t', names = data_header, dtype = None, unpack = True).transpose() # Read space-separated values in engine data file.
sorted_index = np.lexsort((_data_['r'][::-1], _data_['q'], _data_['p']))
_data_ = _data_[sorted_index]
print (_data_)
Ouptut
1 4 15
1 4 14
1 4 13
1 5 19
1 5 20
1 5 16
1 6 21
1 6 22
1 6 18
2 7 20
2 7 19
2 7 17
2 8 13
2 8 15
2 8 14
2 9 22
2 9 21
2 9 16
3 10 13
3 10 16
3 10 19
3 11 14
3 11 13
3 11 20
3 12 16
3 12 15
3 12 17
What could be possibly wrong in this sorting method?