[英]How to convert 3-D Numpy array to Pandas Dataframe?
The problem : I have a 3-D Numpy Array:问题:我有一个 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
Above code yields following output:上面的代码产生以下输出:
1797
(1797, 2)
[23.293915 36.37388 ]
[21.594519 32.874397]
[27.29872 26.798382]
So, there are 1797 data points - each with a X and a Y coordinate and, there are 500 iterations of these 1797 points.因此,有 1797 个数据点 - 每个都有一个 X 和一个 Y 坐标,并且这 1797 个点有 500 次迭代。
I want a DataFrame such that:我想要一个数据帧,这样:
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
I tried techniques mentioned here, but numpy/pandas is really escaping me:我尝试了这里提到的技术,但 numpy/pandas 真的让我望而却步:
Please help me out.请帮帮我。 Hope I am adhering to the question-asking discipline.
希望我遵守提问纪律。
Here's a solution with sample data:这是带有示例数据的解决方案:
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']
)
Output:输出:
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
Try this way:试试这个方法:
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.