问题: 我有一个3-D的Numpy数组:
X
X.shape: (1797, 2, 500)
z=X[..., -1]
print(len(z))
print(z.shape)
count = 0
for bot in z:
print(bot)
count+=1
if count == 3: break
上面的代码产生以下输出:
1797
(1797, 2)
[23.293915 36.37388 ]
[21.594519 32.874397]
[27.29872 26.798382]
因此,有1797个数据点-每个数据点都有X和Y坐标 并且,这1797点有500次迭代。
我想要一个这样的DataFrame:
Index Column | X-coordinate | Y-coordinate
0 | X[0][0][0] | X[0][1][0]
0 | X[1][0][0] | X[1][1][0]
0 | X[2][0][0] | X[2][1][0]
('0') 1797 times
1 | X[0][0][1] | X[0][1][1]
1 | X[1][0][1] | X[1][1][1]
1 | X[2][0][1] | X[2][1][1]
('1' 1797 times)
.
.
.
and so on
till 500
我尝试了这里提到的技术,但是numpy / pandas确实在逃避我:
请帮帮我。 希望我坚持提出问题的准则。
答案 0 :(得分:1)
以下是带有示例数据的解决方案:
a,b,c = X.shape
# in your case
# a,b,c = 1797, 500
pd.DataFrame(X.transpose(1,2,0).reshape(2,-1).T,
index=np.repeat(np.arange(c),a),
columns=['X_coord','Y_coord']
)
输出:
X_coord Y_coord
0 0 3
0 6 9
0 12 15
0 18 21
1 1 4
1 7 10
1 13 16
1 19 22
2 2 5
2 8 11
2 14 17
2 20 23
答案 1 :(得分:0)
尝试这种方式:
index = np.concatenate([np.repeat([i], 1797) for i in range(500)])
df = pd.DataFrame(index=index)
df['X-coordinate'] = X[:, 0, :].T.reshape((-1))
df['Y-coordinate'] = X[:, 1, :].T.reshape((-1))