21 August 2020
Code
1-D array:
# Import library
import numpy as np
# Create 1-D array
x = np.array([1,2,3])
# Append
y = np.insert(arr=x, obj=0, values=999) # Default axis=None
# Output
print('Original array:\n', x, '\n', 'Shape:',x.shape,'\n')
print('After insert:\n', y, '\n', 'Shape:',y.shape,'\n')
Original array:
[1 2 3]
Shape: (3,)
After insert:
[999 1 2 3]
Shape: (4,)
.
2-D array (axis=0):
# Import library
import numpy as np
# Create 2-D array
x = np.array([[1,2],
[3,4]])
# Append
y = np.insert(arr=x, obj=0, values=[888,999], axis=0)
# Output
print('Original array:\n', x, '\n', 'Shape:',x.shape,'\n')
print('After insert:\n', y, '\n', 'Shape:',y.shape,'\n')
Original array:
[[1 2]
[3 4]]
Shape: (2, 2)
After insert:
[[888 999]
[ 1 2]
[ 3 4]]
Shape: (3, 2)
.
2-D array (axis=1):
# Import library
import numpy as np
# Create 2-D array
x = np.array([[1,2],
[3,4]])
# Append
y = np.insert(arr=x, obj=0, values=999, axis=1)
# Output
print('Original array:\n', x, '\n', 'Shape:',x.shape,'\n')
print('After insert:\n', y, '\n', 'Shape:',y.shape,'\n')
Original array:
[[1 2]
[3 4]]
Shape: (2, 2)
After insert:
[[999 1 2]
[999 3 4]]
Shape: (2, 3)
.
Any errors in code above?
Please send a message.