回答編集履歴

1

Update

2021/11/10 21:29

投稿

melian
melian

スコア20590

test CHANGED
@@ -25,3 +25,99 @@
25
25
  7,6.00,5.00,5.00
26
26
 
27
27
  ```
28
+
29
+
30
+
31
+ **追記**
32
+
33
+
34
+
35
+ > 文字列が含まれている場合ではどうしたらいいでしょうか?
36
+
37
+
38
+
39
+ ```python
40
+
41
+ import numpy as np
42
+
43
+
44
+
45
+ # load
46
+
47
+ cols = ('num', 'a', 'b', 'c', 'name')
48
+
49
+ tbl = np.loadtxt(
50
+
51
+ 'data.csv', delimiter=',', skiprows=1,
52
+
53
+ dtype={'names': cols,
54
+
55
+ 'formats': (*(np.int64,)*4, (np.str_, 10))})
56
+
57
+
58
+
59
+ # mean
60
+
61
+ names = np.unique(tbl['name']).tolist()
62
+
63
+ tbl = np.array([tbl[n] for n in cols])[:-1].T.astype(int)
64
+
65
+ result = np.array([tbl[tbl[:,0]==i].mean(axis=0) for i in np.unique(tbl[:,0])])
66
+
67
+
68
+
69
+ # save
70
+
71
+ fmt = ['{:.0f}'] + ['{:.2f}']*3
72
+
73
+ result = [[fmt[m].format(i) for m, i in enumerate(l)] + [names[n]] for n, l in enumerate(result.tolist())]
74
+
75
+ np.savetxt('result.csv', result, delimiter=',', header=','.join(cols), fmt='%s')
76
+
77
+
78
+
79
+ # result.csv
80
+
81
+ # num,a,b,c,name
82
+
83
+ 1,3.00,3.00,6.00,aa
84
+
85
+ 2,7.00,1.00,2.00,bb
86
+
87
+ 3,3.33,5.67,6.67,cc
88
+
89
+ 7,6.00,5.00,5.00,dd
90
+
91
+ ```
92
+
93
+
94
+
95
+ 一方、Pandas を使うと簡単にできます。
96
+
97
+ ```python
98
+
99
+ import pandas as pd
100
+
101
+
102
+
103
+ df = pd.read_csv('data.csv')
104
+
105
+ dfx = df.groupby('num').mean().join(df['name'])
106
+
107
+ dfx.to_csv('result.csv', float_format='%.2f')
108
+
109
+
110
+
111
+ # result.csv
112
+
113
+ num,a,b,c,name
114
+
115
+ 1,3.00,3.00,6.00,aa
116
+
117
+ 2,7.00,1.00,2.00,bb
118
+
119
+ 3,3.33,5.67,6.67,cc
120
+
121
+ 7,6.00,5.00,5.00,dd
122
+
123
+ ```