Table of Contents
ToggleAppending values to an array in NumPy means adding new elements or arrays to an existing NumPy array. This operation involves creating a new array that includes the original elements along with the new ones, as NumPy arrays have fixed sizes and do not support in-place modifications like traditional lists.
Appending values to a 1D array in NumPy involves adding new elements to the end of an existing one-dimensional array. Since NumPy arrays have a fixed size, this operation creates a new array with the original elements plus the newly appended values. To achieve this, we can use the np.append() function in NumPy. Following is the syntax −
numpy.append(arr, values, axis=None)
Where,
In the following example, we are using the np.append() to add elements to a 1D array: first appending a single value, and then multiple values −
# Open Compiler
import numpy as np
# Create an initial 1D array
arr = np.array([1, 2, 3])
# Append a single value
arr_appended_single = np.append(arr, 4)
# Append multiple values
arr_appended_multiple = np.append(arr, [4, 5, 6])
print("Array after appending a single value:", arr_appended_single)
print("Array after appending multiple values:", arr_appended_multiple)
Output:
Following is the output obtained −
Array after appending a single value: [1 2 3 4]
Array after appending multiple values: [1 2 3 4 5 6]
Appending values to a 2D array in NumPy involves adding rows or columns to an existing two-dimensional array. Since NumPy arrays have a fixed size, appending values results in creating a new array that combines the original data with the new data. Let us explore various ways and methods used to append values to a 2D array −
To append rows to a 2D array, we can use the np.vstack() function. This function stacks arrays vertically or concatenate along axis 0. Following is the syntax −
numpy.vstack(tup)
Where, tup is a sequence of arrays to be stacked vertically. All arrays must have the same number of columns.
In this example, we are using the np.vstack() function to append rows to a 2D array, adding new rows beneath the existing ones −
# Open Compiler
import numpy as np
# Create an initial 2D array
arr = np.array([[1, 2], [3, 4]])
# Create an array of rows to append
rows_to_append = np.array([[5, 6], [7, 8]])
# Append rows to the initial array
arr_appended_rows = np.vstack((arr, rows_to_append))
print("Array after appending rows:")
print(arr_appended_rows)
Output:
The result is a new array with the additional rows stacked at the bottom −
Array after appending rows:
[[1 2]
[3 4]
[5 6]
[7 8]]
To append columns to a 2D array, we can use the np.hstack() function. This function stacks arrays horizontally or concatenate along axis 1. Following is the syntax −
numpy.hstack(tup)
Where, tup is a sequence of arrays to be stacked horizontally. All arrays must have the same number of rows.
In the example below, we are using the np.hstack() function to append columns to a 2D array, adding new columns to the right of the existing ones −
# Open Compiler
import numpy as np
# Create an initial 2D array
arr = np.array([[1, 2], [3, 4]])
# Create an array of columns to append
columns_to_append = np.array([[5], [6]])
# Append columns to the initial array
arr_appended_columns = np.hstack((arr, columns_to_append))
print("Array after appending columns:")
print(arr_appended_columns)
Output:
The result is a new array with the additional columns placed alongside the original data −
Array after appending columns:
[[1 2 5]
[3 4 6]]
Appending to multi-dimensional arrays in NumPy involves adding new elements along specified axes. Unlike 1D and 2D arrays, multi-dimensional arrays (e.g., 3D or higher) require careful alignment of the dimensions and axes along which you want to append data.
In the following example, we are using the np.append() function to add values to a 3D array along the first axis −
# Open Compiler import numpy as np # Original 3D array arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Values to append (must match dimensions) values = np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]) # Append values along axis 0 result = np.append(arr, values, axis=0) print(result)
Output:
The resulting array has the new values appended as additional layers on top of the original array −
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]
[[13 14]
[15 16]]]
Appending arrays with different data types in NumPy requires careful handling because NumPy arrays are homogeneous; they must contain elements of the same data type.
When appending arrays with different data types, NumPy will generally perform type coercion or create a new array with a common data type that can contains all the appended data.
In this example, we are using the np.append() to add floating-point values to an integer array −
# Open Compiler import numpy as np # Original array of integers arr = np.array([1, 2, 3]) # Values to append (floating-point) values = np.array([4.5, 5.5]) # Append values result = np.append(arr, values) print(result)
Output:
After executing the above code, we get the following output −
[1. 2. 3. 4.5 5.5]
We can use the np.concatenate() function for combining arrays along a specified axis. It is particularly useful for appending data to existing arrays. Following is the syntax −
numpy.concatenate((a1, a2, ...), axis=0)
Where,
In the following example, we are concatenating two 2D arrays along axis “0” using the np.concatenate() function −
# Open Compiler import numpy as np # Arrays to concatenate arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) # Concatenate along axis 0 result = np.concatenate((arr1, arr2), axis=0) print(result)
Output:
The result produced is as follows −
[[1 2]
[3 4]
[5 6]
[7 8]]
Key Takeaway: Master appending values to NumPy arrays with Vista Academy!
