Python 3.x 使用numpy将列转换为特定行
我试图提取一列并排列成多行。 我的输入:数据Python 3.x 使用numpy将列转换为特定行,python-3.x,numpy,Python 3.x,Numpy,我试图提取一列并排列成多行。 我的输入:数据 -2.74889,1.585,223.60 -2.74889,1.553,228.60 -2.74889,1.423,246.00 -2.74889,1.236,249.10 -2.74889,0.928,243.80 -2.74889,0.710,242.20 -2.74889,0.558,243.50 ... ... ... k = np.reshape(data[:,2], (2,10)) 输出: [[ 223.6 228.
-2.74889,1.585,223.60
-2.74889,1.553,228.60
-2.74889,1.423,246.00
-2.74889,1.236,249.10
-2.74889,0.928,243.80
-2.74889,0.710,242.20
-2.74889,0.558,243.50
...
...
...
k = np.reshape(data[:,2], (2,10))
输出:
[[ 223.6 228.6 246. 249.1 243.8 242.2 243.5 244. 244.8
245.2 ]
[ 224.6 230. 250.7 249.3 244.4 242.1 242.8 243.8 244.7
245.1 ]]
我的问题是如何为每个数字(例如223.6)添加方括号,并将它们保留在一行中
谢谢,
Prasad。重塑形状时,需要扩展阵列的维度 设置
使用附加尺寸重塑
x[:, 2].reshape((-1, 10, 1))
用轴=2展开
x = np.arange(60).reshape(20, 3)
np.expand_dims(x[:, 2].reshape(-1, 10), axis=2)
np.atleast_3d(x[:, 2].reshape(-1, 10))
至少\u 3d
x = np.arange(60).reshape(20, 3)
np.expand_dims(x[:, 2].reshape(-1, 10), axis=2)
np.atleast_3d(x[:, 2].reshape(-1, 10))
这三种产品都是:
array([[[ 2],
[ 5],
[ 8],
[11],
[14],
[17],
[20],
[23],
[26],
[29]],
[[32],
[35],
[38],
[41],
[44],
[47],
[50],
[53],
[56],
[59]]])
你的意思并不完全清楚,但也许是这样的
>>> import numpy as np
>>> data = np.arange(30).reshape(10,3)
>>> data
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23],
[24, 25, 26],
[27, 28, 29]])
>>> data[:, 2, None]
array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17],
[20],
[23],
[26],
[29]])