Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在多维数组中插入列?_Python_Arrays_Numpy - Fatal编程技术网

Python 如何在多维数组中插入列?

Python 如何在多维数组中插入列?,python,arrays,numpy,Python,Arrays,Numpy,这似乎是一个微不足道的问题,但我没有找到我要寻找的答案。 我有一个二维数组,比如: a = np.array([[1,3,5],[2,4,6]]) 还有另一个专栏 b = np.array([9,11]) bt = np.reshape(b, (2,1)) 我想在数组a的零列添加/追加bt列。我尝试使用numpy.insert: tt = np.insert(a,0,bt,axis=1) 但结果是: array([[ 9, 11, 1, 3, 5], [ 9, 11,

这似乎是一个微不足道的问题,但我没有找到我要寻找的答案。 我有一个二维数组,比如:

a = np.array([[1,3,5],[2,4,6]])
还有另一个专栏

b = np.array([9,11])
bt = np.reshape(b, (2,1))
我想在数组
a
的零列添加/追加
bt
列。我尝试使用
numpy.insert

tt = np.insert(a,0,bt,axis=1)
但结果是:

array([[ 9, 11,  1,  3,  5],
       [ 9, 11,  2,  4,  6]])
我想要的是:

array([[ 9, 1,  3,  5],
       [ 11,  2,  4,  6]])

我做错了什么?

您可以直接使用
b

tt = np.insert(a, 0, b, axis=1)
print tt

[[ 9  1  3  5]
 [11  2  4  6]]
或者,如果您开始使用类似于
bt
的形状,请将其转置:

tt = np.insert(a, 0, bt.T, axis=1)
print tt

[[ 9  1  3  5]
 [11  2  4  6]]

可以使用
numpy.column\u stack
执行此操作:

a = np.array([[1,3,5],[2,4,6]])
b = np.array([9,11])
np.column_stack((b, a))

array([[ 9,  1,  3,  5],
       [11,  2,  4,  6]])

作为
np.hstack
的替代方案,您可以使用:


这是一个有趣的解决方案。但我没有完全明白。什么是:>>>c[:,0]=b>>>c[:,1:]=a@Arcticpython它的栏目分配
c[:,0]=b
将在
c
的第一列上分配
b
c[:,1:]=a
将在c的第二列上分配
a
>>> c=np.zeros((a.shape[0],a.shape[1]+1))
>>> c[::,0]=b
>>> c[::,1:]=a
>>> c
array([[  9.,   1.,   3.,   5.],
       [ 11.,   2.,   4.,   6.]])