在numpy中的2d数组中的特定位置插入行? [英] Inserting a row at a specific location in a 2d array in numpy?
本文介绍了在numpy中的2d数组中的特定位置插入行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在numpy中有一个二维数组,我想在其中插入新行.跟随问题 Numpy-将行添加到数组会有所帮助.我们可以使用numpy.vstack
,但是它会在开始或结尾处堆叠.任何人都可以在这方面提供帮助.
I have a 2d array in numpy where I want to insert a new row. Following question Numpy - add row to array can help. We can use numpy.vstack
, but it stacks at the start or at the end. Can anyone please help in this regard.
推荐答案
You are probably looking for 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.]])
这篇关于在numpy中的2d数组中的特定位置插入行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文