How to Append NumPy Arrays Examples

numpy.append() is used to append two or multiple arrays at the end of the specified NumPy array. The NumPy append() function is a built-in function in the NumPy package of python. This function returns a new array after appending the array to the specified array by keeping the original array unchanged.

The numpy.append() appends values along the mentioned axis at the end of the array. In this article, I will explain how to create NumPy arrays using numpy.array(), and then append arrays using append() function with examples. Depending on your usage see if you wanted to concatenate NumPy arrays.

1. Quick Examples of How to Append NumPy Arrays

If you are in a hurry, below are some quick examples of how to append Python NumPy arrays.


# Example 1: Use numpy.append() function
arr = np.array([[0,2,4],[6,8,10]]) 
app_arr = np.append(arr, [13,15,17]) 

# Example 2: Appending array with axis=0
arr = np.array([[0,2,4],[6,8,10]]) 
app_arr=np.append(arr, [[5,7,9],[13,15,17]],axis = 0)

# Example 3: Use Append elements along axis 1
arr = np.array([[0,2,4],[6,8,10]]) 
app_arr=np.append(arr, [[5,7,9],[13,15,17]],axis = 1) 

# Example 4: Use appending array
arr = np.arange(7)  
arr1 = np.arange(9, 14)
arr2 = np.append(arr, arr1)
print("Appended arr2 : ", arr2)

2. Syntax of NumPy Array append()

Following is the syntax of the NumPy array append() function. This function internally using NumPy concatenate() function.


# Syntax of numpy.append()
numpy.append(array, values, axis)

2.1 Parameters of append()

This method allows three parameters :

  • array – Input array, new values are appended to a copy of this array. This parameter is required and plays an important role in numpy.append() function.
  • values – To be appended/added to the array. It must be of the same shape as of array. (excluding axis of appending). If the axis is not defined, then the values can be in any shape and will flatten before use.
  • axis – Axis along which you want to insert the values. By default, the array is flattened.

2.2 Return Value of append()

The return value of this function is the NumPy array which is the copy of the array with the appended passed values to the axis.

2.3 Create a NumPy Array

In order to work with an example, first, let’s create a NumPy array using numpy.array() method. Before going to create the NumPy array we have to import NumPy module.


# Import NumPy
import numpy as np

# Create NumPy Array using NumPy.array() Method
arr = np.array([[0,2,4],[6,8,10]]) 
print(arr)

# Output :
# [[ 0  2  4]
# [ 6  8 10]]

3. Usage of NumPy Array append()

You can use numpy.append() function to add an element in a NumPy array. You can pass the NumPy array and multiple values as arguments to the append() function. It doesn’t modify the existing array but returns a copy of the passed array with given values added.


# Use numpy.append() function
arr = np.array([0,2,4,6,8,10]) 
app_arr = np.append(arr, [13,15,17]) 
print("Appended array:\n",app_arr)

# Output :
# Appended array:
# [ 0  2  4  6  8 10 13 15 17]

You can observe, that from the above code, I have imported NumPy with the alias name np. I have created an array arr using the np.array() function, and also I have taken another array. Then I applied the NumPy array append() function for both two arrays. After that, I assigned a value of the append() function to the app_arr variable. Finally, we have got a flattened array.

3.1 Append NumPy Arrays Along with axis = 0

append() function is also used to append a 2-D NumPy array with some values and array along with axis=0 value. For example,


# Appending array with axis=0
arr = np.array([[0,2,4],[6,8,10]]) 
app_arr=np.append(arr, [[5,7,9],[13,15,17]],axis = 0)
print("Appended array along with axis = 0:\n",app_arr) 

# Output:
# Appended array along with axis = 0:
# [[ 0  2  4]
# [ 6  8 10]
# [ 5  7  9]
# [13 15 17]]

As in the above program, the append() function is used to append 2-Darray with 2-D array(vertically). The appended arrays we can see in the output, the append is done vertically as axis value is 0.

Not using axis=0 on the above example results in the below output.


# Output :
# Appended array:
# [ 0  2  4  6  8 10 5 7 9 13 15 17]

3.2 Append Arrays Along with axis = 1

Let’s see another example of appending 2-D Numpy arrays by using axis=1 value. For example,


# Use Append elements along axis 1
arr = np.array([[0,2,4],[6,8,10]]) 
app_arr=np.append(arr, [[5,7,9],[13,15,17]],axis = 1) 
print("Appended array along with axis = 1:\n",app_arr)

# Output :
# Appended array along with axis = 1:
# [[ 0  2  4  5  7  9]
# [ 6  8 10 13 15 17]]

As in the above program, the append() function is used to append 2-Darray with 2-D array(horizontally). The appended arrays we can see in the output, the append is done horizontally as axis value is 1.

4. Use Appending array

Finally, let’s see how to append two NumPy array variables. numpy.arange() is used to create a NumPy array.


# Use numpy.arange()  
arr = np.arange(7)
print("1st arr : ", arr)
  
# Use numpy.arange()  
arr1 = np.arange(9, 14)
print("2nd arr1 : ", arr1)

# appending the arrays
arr2 = np.append(arr, arr1)
print("\nAppended arr2 : ", arr2)

Yields below output.


# Output
# 1st arr :  [0 1 2 3 4 5 6]
# Shape :  (7,)
# 2nd arr1 :  [ 9 10 11 12 13]
# Shape :  (5,)

# Appended arr2 :  [ 0  1  2  3  4  5  6  9 10 11 12 13]

5. Conclusion

In this article, I have explained how to append NumPy arrays by using numpy.append() methods with examples. When you append NumPy arrays, it doesn’t change the existing array instead it creates a new array by adding an array to the end of the specified array.

Happy Learning !!

You May Also Like

References

Leave a Reply

You are currently viewing How to Append NumPy Arrays Examples