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.append(x, [999]) # Default axis=None
# Output
print('Original array:\n', x, '\n', 'Shape:',x.shape,'\n')
print('After append:\n', y, '\n', 'Shape:',y.shape,'\n')
Original array:
[1 2 3]
Shape: (3,)
After append:
[ 1 2 3 999]
Shape: (4,)
.
2-D array (axis=1):
# Import library
import numpy as np
# Create an array
x = np.array([[1,2],
[10,20]])
# Append
y = np.append(x, [[888,999],[666,777]], axis=1)
# Output
print('Original array:\n', x, '\n', 'Shape:',x.shape,'\n')
print('After append:\n', y, '\n', 'Shape:',y.shape,'\n')
Original array:
[[ 1 2]
[10 20]]
Shape: (2, 2)
After append:
[[ 1 2 888 999]
[ 10 20 666 777]]
Shape: (2, 4)
.
2-D array (axis=0):
# Import library
import numpy as np
# Create an array
x = np.array([[1,2],
[10,20]])
# Append
y = np.append(x, [[888,999],[666,777]], axis=0)
# Output
print('Original array:\n', x, '\n', 'Shape:',x.shape,'\n')
print('After append:\n', y, '\n', 'Shape:',y.shape,'\n')
Original array:
[[ 1 2]
[10 20]]
Shape: (2, 2)
After append:
[[ 1 2]
[ 10 20]
[888 999]
[666 777]]
Shape: (4, 2)
.
Any errors in code above?
Please send a message.