如何按第二列从大到小对两列的二维数组进行排序?
How to sort a 2D array of two columns, from large to small, by the second column?
这是我的代码:
unique_models, count_of_models = np.unique(my_data_frame["model"], return_counts=True)
print(unique_models, count_of_models)
[' A1' ' A2' ' A3' ' A4' ' A5' ' A6' ' A7' ' A8' ' Q2' ' Q3' ' Q5' ' Q7' ' Q8' ' R8'
' RS3' ' RS4' ' RS5' ' RS6' ' RS7' ' S3' ' S4' ' S5' ' S8' ' SQ5' ' SQ7' ' TT']
[1347 1 1929 1381 882 748 122 118 822 1417 877 397 69 28 33 31 29
39 1 18 12 3 4 16 8 336]
representative_models = np.empty((0, 2), int)
other_models = np.empty((0, 2), int)
for models, counts in zip(unique_models, count_of_models):
if counts < 500:
other_models = np.append(other_models, np.array([[models, counts]]), axis=0)
else:
representative_models = np.append(representative_models, np.array([[models, counts]]), axis=0)
print(representative_models[representative_models[:, 1].argsort()])
[[' A1' '1347']
[' A4' '1381']
[' Q3' '1417']
[' A3' '1929']
[' A6' '748']
[' Q2' '822']
[' Q5' '877']
[' A5' '882']]
print(representative_models)
[[' A1' '1347']
[' A3' '1929']
[' A4' '1381']
[' A5' '882']
[' A6' '748']
[' Q2' '822']
[' Q3' '1417']
[' Q5' '877']]
所以正如你所看到的,除了排序之外,一切都成功了,他们只是没有排序。有没有人知道另一种按第二列从大到小排序的方法???
外观示例:
[[' A3' '1929']
[' Q3' '1417']
[' A4' '1381']
[' A1' '1347']
[' A5' '882']
[' Q5' '877']
[' Q2' '822']
[' A6' '748']]
谢谢!
给你:
import numpy as np
data = [[' A1', '1347'],
[' A4', '1381'],
[' Q3', '1417'],
[' A3', '1929'],
[' A6', '748'],
[' Q2', '822'],
[' Q5', '877'],
[' A5', '882']]
indices = np.argsort([int(d[1]) for d in data])
sorted_data = [data[i] for i in indices[::-1]]
假设你的数组如下,你可以这样做:
import numpy as np
representative_models = np.array([[' A1', '1347'],
[' A4', '1381'],
[' Q3', '1417'],
[' A3', '1929'],
[' A6', '748'],
[' Q2', '822'],
[' Q5', '877'],
[' A5', '882']])
# convert last column to int and arg-sort in decreasing order [::-1]
order = np.argsort(representative_models[:, 1].astype(int))[::-1]
# simply index on the input array
result = representative_models[order, :]
print(result)
输出
[[' A3' '1929']
[' Q3' '1417']
[' A4' '1381']
[' A1' '1347']
[' A5' '882']
[' Q5' '877']
[' Q2' '822']
[' A6' '748']]
这是我的代码:
unique_models, count_of_models = np.unique(my_data_frame["model"], return_counts=True)
print(unique_models, count_of_models)
[' A1' ' A2' ' A3' ' A4' ' A5' ' A6' ' A7' ' A8' ' Q2' ' Q3' ' Q5' ' Q7' ' Q8' ' R8'
' RS3' ' RS4' ' RS5' ' RS6' ' RS7' ' S3' ' S4' ' S5' ' S8' ' SQ5' ' SQ7' ' TT']
[1347 1 1929 1381 882 748 122 118 822 1417 877 397 69 28 33 31 29
39 1 18 12 3 4 16 8 336]
representative_models = np.empty((0, 2), int)
other_models = np.empty((0, 2), int)
for models, counts in zip(unique_models, count_of_models):
if counts < 500:
other_models = np.append(other_models, np.array([[models, counts]]), axis=0)
else:
representative_models = np.append(representative_models, np.array([[models, counts]]), axis=0)
print(representative_models[representative_models[:, 1].argsort()])
[[' A1' '1347']
[' A4' '1381']
[' Q3' '1417']
[' A3' '1929']
[' A6' '748']
[' Q2' '822']
[' Q5' '877']
[' A5' '882']]
print(representative_models)
[[' A1' '1347']
[' A3' '1929']
[' A4' '1381']
[' A5' '882']
[' A6' '748']
[' Q2' '822']
[' Q3' '1417']
[' Q5' '877']]
所以正如你所看到的,除了排序之外,一切都成功了,他们只是没有排序。有没有人知道另一种按第二列从大到小排序的方法???
外观示例:
[[' A3' '1929']
[' Q3' '1417']
[' A4' '1381']
[' A1' '1347']
[' A5' '882']
[' Q5' '877']
[' Q2' '822']
[' A6' '748']]
谢谢!
给你:
import numpy as np
data = [[' A1', '1347'],
[' A4', '1381'],
[' Q3', '1417'],
[' A3', '1929'],
[' A6', '748'],
[' Q2', '822'],
[' Q5', '877'],
[' A5', '882']]
indices = np.argsort([int(d[1]) for d in data])
sorted_data = [data[i] for i in indices[::-1]]
假设你的数组如下,你可以这样做:
import numpy as np
representative_models = np.array([[' A1', '1347'],
[' A4', '1381'],
[' Q3', '1417'],
[' A3', '1929'],
[' A6', '748'],
[' Q2', '822'],
[' Q5', '877'],
[' A5', '882']])
# convert last column to int and arg-sort in decreasing order [::-1]
order = np.argsort(representative_models[:, 1].astype(int))[::-1]
# simply index on the input array
result = representative_models[order, :]
print(result)
输出
[[' A3' '1929']
[' Q3' '1417']
[' A4' '1381']
[' A1' '1347']
[' A5' '882']
[' Q5' '877']
[' Q2' '822']
[' A6' '748']]