回答編集履歴

2

indexを修正

2020/09/03 00:45

投稿

bsdfan
bsdfan

スコア4794

test CHANGED
@@ -8,18 +8,42 @@
8
8
 
9
9
 
10
10
 
11
- df = pd.DataFrame({
11
+ df = pd.DataFrame(
12
12
 
13
- 'INDEX': [201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604],
13
+ {'番号': [4, 16, 15, 10, 11, 7, 5, 9, 13, 1, 3, 8, 14, 12, 6, 2, 2, 8, 1, 15, 7, 4, 11, 5, 13, 9, 6, 3, 12, 10, 16, 14, 1, 6, 5, 4, 15, 16, 10, 2, 9, 12, 3, 8, 13, 7, 14, 11, 14, 11, 5, 7, 6, 2, 13, 3, 4, 12, 9, 1, 10],
14
14
 
15
- '番号': [4, 16, 15, 10, 11, 7, 5, 9, 13, 1, 3, 8, 14, 12, 6, 2, 2, 8, 1, 15, 7, 4, 11, 5, 13, 9, 6, 3, 12, 10, 16, 14, 1, 6, 5, 4, 15, 16, 10, 2, 9, 12, 3, 8, 13, 7, 14, 11, 14, 11, 5, 7, 6, 2, 13, 3, 4, 12, 9, 1, 10],
15
+ '平均': [0.411331, 0.472256, 0.758492, 0.704865, 0.722583, 0.502218, 0.422376, 0.142984, 0.166259, 0.063561, 0.170293, 0.10557, 0.186378, 0.058321, 0.103821, 0.241742, 0.470611, 0.783678, 0.298781, 0.302518, 0.710182, 0.175024, 0.402872, 0.463226, 0.373702, 0.043652, 0.329344, 0.331674, 0.203614, 0.094564, 0.200508, 0.093363, 0.794446, 0.801182, 0.84976, 0.804972, 0.240848, 0.273469, 0.327639, 0.409137, 0.183374, 0.485936, 0.258582, 0.786969, 0.323141, 0.1709, 0.166577, 0.02937, 0.773029, 0.659245, 0.572293, 0.834579, 0.23578, 0.217652, 0.684847, 0.254697, 0.40418, 0.215965, 0.409363, 0.362527, 0.367208]},
16
16
 
17
- '平均': [0.411331, 0.472256, 0.758492, 0.704865, 0.722583, 0.502218, 0.422376, 0.142984, 0.166259, 0.063561, 0.170293, 0.10557, 0.186378, 0.058321, 0.103821, 0.241742, 0.470611, 0.783678, 0.298781, 0.302518, 0.710182, 0.175024, 0.402872, 0.463226, 0.373702, 0.043652, 0.329344, 0.331674, 0.203614, 0.094564, 0.200508, 0.093363, 0.794446, 0.801182, 0.84976, 0.804972, 0.240848, 0.273469, 0.327639, 0.409137, 0.183374, 0.485936, 0.258582, 0.786969, 0.323141, 0.1709, 0.166577, 0.02937, 0.773029, 0.659245, 0.572293, 0.834579, 0.23578, 0.217652, 0.684847, 0.254697, 0.40418, 0.215965, 0.409363, 0.362527, 0.367208]
18
-
19
- })
17
+ index=[201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201907010601, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201905010402, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010606, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604, 201907010604])
20
18
 
21
19
 
22
20
 
23
- df = df.sort_values('平均', ascending=False).groupby('INDEX').head(3).sort_index()
21
+ df = df.sort_values('平均', ascending=False).groupby(level=0).head(3).sort_index()
22
+
23
+ # 番号 平均
24
+
25
+ # 201905010402 8 0.783678
26
+
27
+ # 201905010402 7 0.710182
28
+
29
+ # 201905010402 2 0.470611
30
+
31
+ # 201907010601 15 0.758492
32
+
33
+ # 201907010601 11 0.722583
34
+
35
+ # 201907010601 10 0.704865
36
+
37
+ # 201907010604 7 0.834579
38
+
39
+ # 201907010604 14 0.773029
40
+
41
+ # 201907010604 13 0.684847
42
+
43
+ # 201907010606 5 0.849760
44
+
45
+ # 201907010606 4 0.804972
46
+
47
+ # 201907010606 6 0.801182
24
48
 
25
49
  ```

1

一部修正

2020/09/03 00:44

投稿

bsdfan
bsdfan

スコア4794

test CHANGED
@@ -1,4 +1,4 @@
1
- 先にsortしてから、groupbyすればいいです。
1
+ 先にsortしてから、groupbyして、headすればいいです。
2
2
 
3
3
 
4
4
 
@@ -20,6 +20,6 @@
20
20
 
21
21
 
22
22
 
23
- df.sort_values('平均', ascending=False).groupby('INDEX').head(3).sort_index()
23
+ df = df.sort_values('平均', ascending=False).groupby('INDEX').head(3).sort_index()
24
24
 
25
25
  ```