numpy.append() in Python
The numpy.append() function appends the values to the end of an array.
Syntax
numpy.append(arr, values, axis=None)
Parameter
The numpy.append() function consists of three parameters, which are as follows:
arr : This parameter represents the values that are appended to a copy of this array.
values: These values are appended to a copy of arr. It must be of the correct shape. If the ‘axis’ parameter is not specified, values can be any shape and will be flattened before use.
axis: This parameter represents the axis along which values are appended. If the axis is not given, both arr and values are flattened before use.
Return
This function returns a copy of ‘arr’ parameter with values appended to the axis.
Example 1
# Python program explaining
# numpy.append() function
import numpy as np
inp_arr = np.array([[11,12,13],[14,15,16]])
print ('Input array:')
print (inp_arr )
print ('\nAppending the elements to array:')
print (np.append(inp_arr, [17,18,19]))
Output
Input array:
[[11 12 13]
[14 15 16]]
Appending the elements to array:
[11 12 13 14 15 16 17 18 19]
Example 2
# Python program explaining
# numpy.append() function
import numpy as np
inp_arr = np.array([[11,12,13],[14,15,16]])
print ('Input array:')
print (inp_arr )
print ('\nAppending the elements along axis=0:')
print (np.append(inp_arr, [[17,18,19]],axis = 0))
print ('\nAppending elements along axis=1:')
print (np.append(inp_arr, [[15,15,15],[17,18,19]],axis = 1))
Output
Input array:
[[11 12 13]
[14 15 16]]
Appending the elements along axis=0:
[[11 12 13]
[14 15 16]
[17 18 19]]
Appending elements along axis=1:
[[11 12 13 15 15 15]
[14 15 16 17 18 19]]