Appending values at the end of an NumPy array
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() and numpy.concatenate(). This function can help us to append a single value as well as multiple values at the end of the array. In this article, we will also see how to append elements to the NumPy array.
Appending Values at the End of an NumPy Array
- Appending a Single Value to a 1D Array
- Appending Another Array at the End of a 1D Array
- Appending Values at the End Using Concatenation
- Appending with a Different Array Type
- Appending Using List Comprehension and
numpy.concatenate
- Appending Values at the End of the N-Dimensional Array
Appending a Single Value to a 1D Array
For a 1D array, using the axis argument is not necessary as the array is flattened by default.
python3
# importing the module import numpy as np # creating an array arr = np.array([ 1 , 8 , 3 , 3 , 5 ]) print ( 'Original Array : ' , arr) # appending to the array arr = 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]
Appending Another Array at the End of a 1D Array
You may pass a list or an array to the append function, the result will be the same.
python3
# importing the module import numpy as np # creating an array arr1 = np.array([ 1 , 2 , 3 ]) print ( 'First array is : ' , arr1) # creating another array arr2 = np.array([ 4 , 5 , 6 ]) print ( 'Second array is : ' , arr2) # appending arr2 to arr1 arr = 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]
Appending Values at the End Using Concatenation
In this example, two 2D arrays, arr1
and arr2
, are vertically stacked using np.concatenate()
along the 0th axis, resulting in a combined 2D array.
Python3
# importing the module import numpy as np arr1 = np.array([[ 1 , 2 ], [ 3 , 4 ]]) arr2 = np.array([[ 5 , 6 ]]) combined = np.concatenate((arr1, arr2), axis = 0 ) print (combined) |
Output:
[[1 2]
[3 4]
[5 6]]
Appending with a Different Array Type
In this example, a 1D integer array arr
and a 1D float array arr_float
are appended together using np.append()
, resulting in an upcasted float array as the output.
Python3
# importing the module import numpy as np arr = np.array([ 1 , 2 , 3 ]) arr_float = np.array([ 4.0 , 5.0 ]) combined = np.append(arr, arr_float) print (combined) # Output: [1. 2. 3. 4. 5.] |
Output:
[1. 2. 3. 4. 5.]
Appending Using List Comprehension and numpy.concatenate
In this example, multiple arrays, including arr
and two arrays from values_to_append
, are concatenated using list comprehension and np.concatenate()
, producing a single combined array.
Python3
# importing the module import numpy as np arr = np.array([ 1 , 2 , 3 , 4 , 5 ]) values_to_append = [np.array([ 6 , 7 ]), np.array([ 8 , 9 ])] combined = np.concatenate([arr] + values_to_append) print (combined) |
Output:
[1 2 3 4 5 6 7 8 9]
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.
python3
# importing the module import numpy as np # create an array arr = np.arange( 1 , 13 ).reshape( 2 , 6 ) print ( 'Original Array' ) print (arr, '\n' ) # create another array which is # to be appended column-wise col = 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 wise row = 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]]