回答編集履歴
1
補足を追加
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
`numpy.argsort`を使う。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
```Python
|
2
6
|
|
3
7
|
import numpy as np
|
@@ -23,3 +27,95 @@
|
|
23
27
|
print(arr1)
|
24
28
|
|
25
29
|
```
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
```result
|
34
|
+
|
35
|
+
[[5 1]
|
36
|
+
|
37
|
+
[3 4]
|
38
|
+
|
39
|
+
[4 8]]
|
40
|
+
|
41
|
+
基準となる列のndarray: [5 3 4]
|
42
|
+
|
43
|
+
その列で昇順ソートした場合のインデックス: [1 2 0]
|
44
|
+
|
45
|
+
[[3 4]
|
46
|
+
|
47
|
+
[4 8]
|
48
|
+
|
49
|
+
[5 1]]
|
50
|
+
|
51
|
+
```
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
面倒くさいなら、いったんpandasに変換してソートしてからndarrayに戻す。
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
```Python
|
60
|
+
|
61
|
+
import numpy as np
|
62
|
+
|
63
|
+
import pandas as pd
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
arr = np.array([[5, 1], [3, 4], [4, 8]])
|
68
|
+
|
69
|
+
print(arr)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
df = pd.DataFrame(arr)
|
74
|
+
|
75
|
+
print(df)
|
76
|
+
|
77
|
+
df.sort_values(0, inplace=True)
|
78
|
+
|
79
|
+
print(df)
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
arr1 = df.values
|
84
|
+
|
85
|
+
print(arr1)
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
```result
|
92
|
+
|
93
|
+
[[5 1]
|
94
|
+
|
95
|
+
[3 4]
|
96
|
+
|
97
|
+
[4 8]]
|
98
|
+
|
99
|
+
0 1
|
100
|
+
|
101
|
+
0 5 1
|
102
|
+
|
103
|
+
1 3 4
|
104
|
+
|
105
|
+
2 4 8
|
106
|
+
|
107
|
+
0 1
|
108
|
+
|
109
|
+
1 3 4
|
110
|
+
|
111
|
+
2 4 8
|
112
|
+
|
113
|
+
0 5 1
|
114
|
+
|
115
|
+
[[3 4]
|
116
|
+
|
117
|
+
[4 8]
|
118
|
+
|
119
|
+
[5 1]]
|
120
|
+
|
121
|
+
```
|