在numpy的2d数组中的特定位置插入一行?

Sha*_*han 24 python numpy

我有一个ndy的2d数组,我想插入一个新行.以下问题Numpy - 向数组添加行可以提供帮助.我们可以使用numpy.vstack,但它在开始或结束时堆叠.任何人都可以在这方面提供帮助.

mac*_*mac 47

你可能正在寻找 numpy.insert

>>> import numpy as np
>>> a = np.zeros((2, 2))
>>> a
array([[ 0.,  0.],
       [ 0.,  0.]])
# In the following line 1 is the index before which to insert, 0 is the axis.
>>> np.insert(a, 1, np.array((1, 1)), 0)  
array([[ 0.,  0.],
       [ 1.,  1.],
       [ 0.,  0.]])
>>> np.insert(a, 1, np.array((1, 1)), 1)
array([[ 0.,  1.,  0.],
       [ 0.,  1.,  0.]])
Run Code Online (Sandbox Code Playgroud)