15
我在numpy中有一個2d數組,我想插入一個新行。以下問題Numpy - add row to array可以提供幫助。我們可以使用numpy.vstack
,但它在開始或結束時堆疊。任何人都可以請這方面的幫助。在numpy中的2d數組中的特定位置插入一行?
我在numpy中有一個2d數組,我想插入一個新行。以下問題Numpy - add row to array可以提供幫助。我們可以使用numpy.vstack
,但它在開始或結束時堆疊。任何人都可以請這方面的幫助。在numpy中的2d數組中的特定位置插入一行?
您可能正在尋找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.]])
偉大!謝謝 – Shan
在版本1.4.2中似乎沒有工作 – Bogdan