简体   繁体   English

在numpy的二维数组中的特定位置插入一行?

[英]Inserting a row at a specific location in a 2d array in numpy?

I have a 2d array in numpy where I want to insert a new row.我在 numpy 中有一个二维数组,我想在其中插入一个新行。 Following question Numpy - add row to array can help.以下问题Numpy - add row to array可以提供帮助。 We can use numpy.vstack , but it stacks at the start or at the end.我们可以使用numpy.vstack ,但它在开始或结束时堆叠。 Can anyone please help in this regard.任何人都可以在这方面提供帮助。

You are probably looking for numpy.insert您可能正在寻找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.]])

the insert does not seem to work with me. 该插件似乎不适用于我。 Iam using numpy 1.17.2. Iam使用numpy 1.17.2。

however, this worked for me if you want to insert rows for example 但是,如果您想插入行,这对我有用

array[index] = (1,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM