回答編集履歴

1

修正

2021/02/04 05:50

投稿

jeanbiego
jeanbiego

スコア3966

test CHANGED
@@ -22,33 +22,39 @@
22
22
 
23
23
 
24
24
 
25
- df = pd.read_table(io.StringIO(data), delimiter="\s+" ,index_col="state")
25
+ df = pd.read_table(io.StringIO(data), delimiter="\s+", index_col="state")
26
+
27
+
28
+
29
+
30
+
31
+ # ここから処理
32
+
33
+ df = df.reset_index()
26
34
 
27
35
  print(df)
28
36
 
29
- # a b c
37
+ # state a b c
30
38
 
31
- # state
39
+ # 0 0 0 0 1
32
40
 
33
- # 0 0 0 1
41
+ # 1 0 0 1 1
34
42
 
35
- # 0 0 1 1
43
+ # 2 1 0 1 1
36
44
 
37
- # 1 0 1 1
38
-
39
- # 1 1 1 0
45
+ # 3 1 1 1 0
40
46
 
41
47
  results = {}
42
48
 
43
- for i in [0,1]:
49
+ for col in df.columns:
44
50
 
45
- results[i] = {}
51
+ if col == "state":
46
52
 
47
- for col in df.columns:
53
+ continue
48
54
 
49
- ratio = df.groupby(col).groups[i]
55
+ ratio = df.groupby(col).mean()
50
56
 
51
- results[i][col] = sum(ratio)/len(ratio)
57
+ results[col] = ratio["state"]
52
58
 
53
59
  df_results = pd.DataFrame(results)
54
60