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

前端 未结 1 550
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 07:11

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

1条回答
  •  醉梦人生
    2020-12-13 07:31

    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.]])
    

    0 讨论(0)
提交回复
热议问题