You can easily manipulate an array using the numpy package. You have to just convert the existing array to numpy and then use the inbuilt function. Then numpy append function is one of them. In this post you will implement this function through steps.
Syntax of the Numpy append () function
numpy.append(arr, values, axis=None)
Below is the explanation of the parameters.
arr: The input array.
values: The values to be appended. This can be a single value or an array of values.
axis (optional): The axis along which values are appended. If not specified, the array is flattened before appending.
Steps to implement Numpy Append function
Below are the steps to implement the numpy append function
Step 1: Import NumPy
In the first steps import the numpy module in your code. You can import it using the import statement.
import numpy as np
Step 2: Create an Array
Next, create the initial array to which you want to append values.
original_array = np.array([10, 20, 30])
Step 3: Use numpy.append() function
Use the numpy.append()
function to append values to the end of the array. This function takes three arguments
For single value
new_value = 40
updated_array = np.append(original_array, new_value)
Appending Multiple values
new_values = [40, 50, 60]
updated_array = np.append(original_array, new_values)
Step 4: Print or Display the Updated Array
Display the array after appending the new value or arrays at the end of the existing numpy array.
print(updated_array)
Here’s a complete code for the above steps.
import numpy as np
# Step 2: Create an Array
original_array = np.array([10, 20, 30])
# Step 3: Use numpy.append()
new_value = 40
updated_array = np.append(original_array, new_value)
# Step 4: Print or Use the Updated Array
print(updated_array)
Output
[10 20 30 40]
Conclusion
The function numpy append is very useful function. It allows you to append single or multiple values at the end of the array without modifying the original array. The above steps are the easy implementation of the np.append() function.
Leave a Reply