回答編集履歴
2
結局追記
answer
CHANGED
@@ -39,4 +39,37 @@
|
|
39
39
|
B = np.r_[B, A1]
|
40
40
|
|
41
41
|
print((B.T).shape) # ②(492, 2)だが正解は?
|
42
|
+
```
|
43
|
+
|
44
|
+
結局は
|
45
|
+
---
|
46
|
+
|
47
|
+
かなり推測(エスパー)入っていますが、以下が求めるコードになるかと思われます。
|
48
|
+
```Python
|
49
|
+
import pandas as pd
|
50
|
+
import numpy as np
|
51
|
+
|
52
|
+
dat = np.arange(164*14).reshape(164,-1)
|
53
|
+
L5 = ['W-A0', 'W-B0', 'W-F0', 'W-C1', 'W-E1', 'W-A2', 'W-B2', 'W-D2', 'W-F2', 'W-C3', 'W-E3',
|
54
|
+
'W-A4', 'W-B4', 'W-F4']
|
55
|
+
|
56
|
+
Ds1 = pd.DataFrame(dat,columns=L5)
|
57
|
+
Ds2 = pd.DataFrame(dat,columns=L5)
|
58
|
+
print(Ds1.shape,Ds2.shape) # 質問文よりどちらも(164, 14) (164, 14)
|
59
|
+
|
60
|
+
dL = list(range(100, 2100, 200)) # [100,300,500,...]
|
61
|
+
#B = np.zeros((0, Ds1.values.shape[0])) # 不要
|
62
|
+
|
63
|
+
for i in range(0,2):
|
64
|
+
# Df1とDf2の'W=??'列 同士(+dL)を足し合わせる shape(164,xx) xx=3,2
|
65
|
+
col1 = Ds1.columns.str.contains(r'(?=.*W)(?=.*%s)' % i)
|
66
|
+
col2 = Ds2.columns.str.contains(r'(?=.*W)(?=.*%s)' % i)
|
67
|
+
A1 = dL[i] + Ds2.loc[:, col2].values + Ds1.loc[:, col1].values
|
68
|
+
|
69
|
+
if i == 0:
|
70
|
+
B = A1.copy()
|
71
|
+
else:
|
72
|
+
B = np.c_[B, A1] # 列方向に足す!
|
73
|
+
|
74
|
+
print(B.shape) # 欲しいshapeは(164, 5)
|
42
75
|
```
|
1
コード修正
answer
CHANGED
@@ -2,16 +2,18 @@
|
|
2
2
|
そのままでは無理なので[numpy.resize](https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.resize.html)で足りない方の列を0埋めで増やしてやるしかないと思います。
|
3
3
|
参考:[Python: Resize an existing array and fill with zeros](https://stackoverflow.com/questions/9251635/python-resize-an-existing-array-and-fill-with-zeros)
|
4
4
|
|
5
|
+
なお、2点ほど疑問点をコメント中に記載しましたので回答願います。
|
5
6
|
```Python
|
6
7
|
import pandas as pd
|
7
8
|
import numpy as np
|
8
9
|
|
9
|
-
dat = np.arange(
|
10
|
+
dat = np.arange(164*14).reshape(164,-1)
|
10
11
|
L5 = ['W-A0', 'W-B0', 'W-F0', 'W-C1', 'W-E1', 'W-A2', 'W-B2', 'W-D2', 'W-F2', 'W-C3', 'W-E3',
|
11
12
|
'W-A4', 'W-B4', 'W-F4']
|
12
13
|
|
13
14
|
Ds1 = pd.DataFrame(dat,columns=L5)
|
14
15
|
Ds2 = pd.DataFrame(dat,columns=L5)
|
16
|
+
print(Ds1.shape,Ds2.shape) # 質問文よりどちらも(164, 14) (164, 14)
|
15
17
|
|
16
18
|
dL = list(range(100, 2100, 200))
|
17
19
|
#B = np.zeros((0, Ds1.values.shape[0])) # 不要
|
@@ -20,20 +22,21 @@
|
|
20
22
|
col1 = Ds1.columns.str.contains(r'(?=.*W)(?=.*%s)' % i)
|
21
23
|
col2 = Ds2.columns.str.contains(r'(?=.*W)(?=.*%s)' % i)
|
22
24
|
A1 = dL[i] + Ds2.loc[:, col2].values + Ds1.loc[:, col1].values
|
23
|
-
|
25
|
+
|
24
26
|
# copy()が必要
|
25
27
|
# Numpy, the array doesn't have its own data?
|
26
28
|
# https://stackoverflow.com/questions/23253144/numpy-the-array-doesnt-have-its-own-data
|
27
|
-
|
29
|
+
A1 = A1.reshape(1, -1).copy()
|
30
|
+
print(A1.shape) # ①i=0で(1, 492)、i=1で(1, 328)だが正しいか?
|
28
31
|
|
29
32
|
if i == 0:
|
30
|
-
B =
|
33
|
+
B = A1.copy()
|
31
34
|
else: # 列数を大きい方に合わせる
|
32
|
-
if B.shape[1] <
|
35
|
+
if B.shape[1] < A1.shape[1]:
|
33
|
-
B.resize((B.shape[0],
|
36
|
+
B.resize((B.shape[0],A1.shape[1]))
|
34
|
-
elif
|
37
|
+
elif A1.shape[1] < B.shape[1]:
|
35
|
-
|
38
|
+
A1.resize((1,B.shape[1]))
|
36
|
-
B = np.r_[B,
|
39
|
+
B = np.r_[B, A1]
|
37
40
|
|
38
|
-
print(B.T)
|
41
|
+
print((B.T).shape) # ②(492, 2)だが正解は?
|
39
42
|
```
|