回答編集履歴

2

結局追記

2018/11/20 11:27

投稿

can110
can110

スコア38266

test CHANGED
@@ -81,3 +81,69 @@
81
81
  print((B.T).shape) # ②(492, 2)だが正解は?
82
82
 
83
83
  ```
84
+
85
+
86
+
87
+ 結局は
88
+
89
+ ---
90
+
91
+
92
+
93
+ かなり推測(エスパー)入っていますが、以下が求めるコードになるかと思われます。
94
+
95
+ ```Python
96
+
97
+ import pandas as pd
98
+
99
+ import numpy as np
100
+
101
+
102
+
103
+ dat = np.arange(164*14).reshape(164,-1)
104
+
105
+ L5 = ['W-A0', 'W-B0', 'W-F0', 'W-C1', 'W-E1', 'W-A2', 'W-B2', 'W-D2', 'W-F2', 'W-C3', 'W-E3',
106
+
107
+ 'W-A4', 'W-B4', 'W-F4']
108
+
109
+
110
+
111
+ Ds1 = pd.DataFrame(dat,columns=L5)
112
+
113
+ Ds2 = pd.DataFrame(dat,columns=L5)
114
+
115
+ print(Ds1.shape,Ds2.shape) # 質問文よりどちらも(164, 14) (164, 14)
116
+
117
+
118
+
119
+ dL = list(range(100, 2100, 200)) # [100,300,500,...]
120
+
121
+ #B = np.zeros((0, Ds1.values.shape[0])) # 不要
122
+
123
+
124
+
125
+ for i in range(0,2):
126
+
127
+ # Df1とDf2の'W=??'列 同士(+dL)を足し合わせる shape(164,xx) xx=3,2
128
+
129
+ col1 = Ds1.columns.str.contains(r'(?=.*W)(?=.*%s)' % i)
130
+
131
+ col2 = Ds2.columns.str.contains(r'(?=.*W)(?=.*%s)' % i)
132
+
133
+ A1 = dL[i] + Ds2.loc[:, col2].values + Ds1.loc[:, col1].values
134
+
135
+
136
+
137
+ if i == 0:
138
+
139
+ B = A1.copy()
140
+
141
+ else:
142
+
143
+ B = np.c_[B, A1] # 列方向に足す!
144
+
145
+
146
+
147
+ print(B.shape) # 欲しいshapeは(164, 5)
148
+
149
+ ```

1

コード修正

2018/11/20 11:27

投稿

can110
can110

スコア38266

test CHANGED
@@ -5,6 +5,8 @@
5
5
  参考:[Python: Resize an existing array and fill with zeros](https://stackoverflow.com/questions/9251635/python-resize-an-existing-array-and-fill-with-zeros)
6
6
 
7
7
 
8
+
9
+ なお、2点ほど疑問点をコメント中に記載しましたので回答願います。
8
10
 
9
11
  ```Python
10
12
 
@@ -14,7 +16,7 @@
14
16
 
15
17
 
16
18
 
17
- dat = np.arange(42).reshape(3,-1)
19
+ dat = np.arange(164*14).reshape(164,-1)
18
20
 
19
21
  L5 = ['W-A0', 'W-B0', 'W-F0', 'W-C1', 'W-E1', 'W-A2', 'W-B2', 'W-D2', 'W-F2', 'W-C3', 'W-E3',
20
22
 
@@ -25,6 +27,8 @@
25
27
  Ds1 = pd.DataFrame(dat,columns=L5)
26
28
 
27
29
  Ds2 = pd.DataFrame(dat,columns=L5)
30
+
31
+ print(Ds1.shape,Ds2.shape) # 質問文よりどちらも(164, 14) (164, 14)
28
32
 
29
33
 
30
34
 
@@ -42,7 +46,7 @@
42
46
 
43
47
  A1 = dL[i] + Ds2.loc[:, col2].values + Ds1.loc[:, col1].values
44
48
 
45
-
49
+
46
50
 
47
51
  # copy()が必要
48
52
 
@@ -50,28 +54,30 @@
50
54
 
51
55
  # https://stackoverflow.com/questions/23253144/numpy-the-array-doesnt-have-its-own-data
52
56
 
53
- A2 = A1.reshape(1, -1).copy()
57
+ A1 = A1.reshape(1, -1).copy()
58
+
59
+ print(A1.shape) # ①i=0で(1, 492)、i=1で(1, 328)だが正しいか?
54
60
 
55
61
 
56
62
 
57
63
  if i == 0:
58
64
 
59
- B = A2.copy()
65
+ B = A1.copy()
60
66
 
61
67
  else: # 列数を大きい方に合わせる
62
68
 
63
- if B.shape[1] < A2.shape[1]:
69
+ if B.shape[1] < A1.shape[1]:
64
70
 
65
- B.resize((B.shape[0],A2.shape[1]))
71
+ B.resize((B.shape[0],A1.shape[1]))
66
72
 
67
- elif A2.shape[1] < B.shape[1]:
73
+ elif A1.shape[1] < B.shape[1]:
68
74
 
69
- A2.resize((1,B.shape[1]))
75
+ A1.resize((1,B.shape[1]))
70
76
 
71
- B = np.r_[B, A2]
77
+ B = np.r_[B, A1]
72
78
 
73
79
 
74
80
 
75
- print(B.T)
81
+ print((B.T).shape) # ②(492, 2)だが正解は?
76
82
 
77
83
  ```