Appending values at the end of an NumPy array - GeeksforGeeks

02 Sep, 2020
Let us see how to append values at the end of a NumPy array. Adding values at the end of the array is a necessary task especially when the data is not fixed and is prone to change. For this task we can use numpy.append(). This function can help us to append a single value as well as multiple values at the end of the array.
Syntax : numpy.append(array, values, axis = None)Parameters :
array : Input array.
values : values to be added in the array.
axis : Axis along which we want to insert the values.
Returns : An copy of array with values being appended at the end as per the mentioned objectalong a given axis.
Examples 1 : Appending a single value to a 1D array. For 1D array, using the axis argument is not necessary as the array is flattened by default.
# importing the moduleimport numpy as np  # creating an arrayarr = np.array([1, 8, 3, 3, 5])print('Original Array : ', arr)  # appending to the arrayarr = np.append(arr, [7])print('Array after appending : ', arr)
Output :
Original Array :  [1 8 3 3 5]
Array after appending :  [1 8 3 3 5 7]

Example 2 : Appending another array at the end of 1D array. You may pass a list or an array to the append function, the result will be the same.
# importing the moduleimport numpy as np  # creating an arrayarr1 = np.array([1, 2, 3])print('First array is : ', arr1)  # creating another arrayarr2 = np.array([4, 5, 6])print('Second array is : ', arr2)  # appending arr2 to arr1arr = np.append(arr1, arr2)print('Array after appending : ', arr)
Output :
First array is :  [1 2 3]
Second array is :  [4 5 6]
Array after appending :  [1 2 3 4 5 6]

Example 3 : Appending values at the end of the n-dimensional array. It is important that the dimensions of both the array matches otherwise it will give an error.
# importing the moduleimport numpy as np  # create an arrayarr = np.arange(1, 13).reshape(2, 6)print('Original Array')print(arr, '\n')  # create another array which is# to be appended column-wisecol = np.arange(5, 11).reshape(1, 6)print('Array to be appended column wise')print(col)arr_col = np.append(arr, col, axis = 0)print('Array after appending the values column wise')print(arr_col, '\n')  # create an array which is# to be appended row wiserow = np.array([1, 2]).reshape(2, 1)print('Array to be appended row wise')print(row)arr_row = np.append(arr, row, axis = 1)print('Array after appending the values row wise')print(arr_row)
Output :
Original Array
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]] 

Array to be appended column wise
[[ 5  6  7  8  9 10]]
Array after appending the values column wise
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [ 5  6  7  8  9 10]] 

Array to be appended row wise
[[1]
 [2]]
Array after appending the values row wise
[[ 1  2  3  4  5  6  1]
 [ 7  8  9 10 11 12  2]]

Python numpy-arrayManipulation
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()