In [1]:
import numpy as np
In [2]:
a = np.arange(4)
print(a)
In [3]:
print(np.insert(a, 2, 100))
In [4]:
print(np.insert(a, 1, [100, 101, 102]))
In [5]:
print(np.insert(a, [0, 2, 4], [100, 101, 102]))
In [6]:
_a = a.copy()
_a[1] = 100
print(_a)
In [7]:
_a = a.copy()
_a[1:3] = [100, 101]
print(_a)
In [8]:
# _a = a.copy()
# _a[1] = [100, 101, 102]
# print(_a)
# ValueError: setting an array element with a sequence.
In [9]:
_a = np.insert(a, 1, [100, 101, 102])
_a = np.delete(_a, 4)
print(_a)
In [10]:
a = np.arange(12).reshape((3, 4))
print(a)
In [11]:
print(np.insert(a, 2, 100))
In [12]:
print(np.insert(a, 2, 100, axis=0))
In [13]:
b1 = np.arange(100, 104)
print(b1)
In [14]:
print(np.insert(a, 1, b1, axis=0))
In [15]:
print(np.insert(a, 3, b1, axis=0))
In [16]:
print(np.insert(a, [0, 2], b1, axis=0))
In [17]:
b2 = np.arange(100, 112).reshape((3, 4))
print(b2)
In [18]:
print(np.insert(a, 2, b2, axis=0))
In [19]:
print(np.insert(a, 2, b2[2], axis=0))
In [20]:
print(np.insert(a, [0, 2, 3], b2, axis=0))
In [21]:
print(np.insert(a, range(3), b2, axis=0))
In [22]:
print(np.vstack((a, b1)))
In [23]:
print(np.vstack((b2, a)))
In [24]:
_a = a.copy()
_a[2] = b1
print(_a)
In [25]:
_a = a.copy()
_a[1] = b2[1]
print(_a)
In [26]:
_a = a.copy()
_a[1:] = b2[[0, 2]]
print(_a)
In [27]:
print(a)
In [28]:
print(np.insert(a, 1, 100, axis=1))
In [29]:
c1 = np.arange(100, 103)
print(c1)
In [30]:
print(np.insert(a, 1, c1, axis=1))
In [31]:
print(np.insert(a, 3, c1, axis=1))
In [32]:
c2 = np.arange(100, 106).reshape((3, 2))
print(c2)
In [33]:
# print(np.insert(a, 1, c2, axis=1))
# ValueError: could not broadcast input array from shape (2,3) into shape (3,3)
In [34]:
print(np.insert(a, [1], c2, axis=1))
In [35]:
print(np.insert(a, [0, 2], c2, axis=1))
In [36]:
print(c1)
In [37]:
print(np.insert(a, 1, c1, axis=1))
In [38]:
print(np.insert(a, [1], c1, axis=1))
In [39]:
print(np.insert(a, [1, 3, 4], c1, axis=1))
In [40]:
_c1 = c1.reshape((3, 1))
print(_c1)
In [41]:
print(np.insert(a, 1, _c1, axis=1))
In [42]:
print(np.insert(a, [1], _c1, axis=1))
In [43]:
print(np.insert(a, [1, 3, 4], _c1, axis=1))
In [44]:
# print(np.hstack((a, c1)))
# ValueError: all the input arrays must have same number of dimensions
In [45]:
print(_c1)
In [46]:
print(np.hstack((a, _c1)))
In [47]:
print(np.hstack((_c1, a)))
In [48]:
print(np.hstack((a, c2)))
In [49]:
print(np.hstack((c2, a)))
In [50]:
_a = a.copy()
_a[:, 1] = c1
print(_a)
In [51]:
_a = a.copy()
_a[:, :2] = c2
print(_a)
In [52]:
_a = a.copy()
_a[:, [0, 3]] = c2
print(_a)