In this article, we will discuss how to append elements at the end on a Numpy Array in python using numpy.append()
Overview of numpy.append()
Python’s Numpy module provides a function to append elements to the end of a Numpy Array.
numpy.append(arr, values, axis=None)
Arguments:
 arr: array_like
 Given values will be added in copy of this array.
 values: array_like
 Values that needs to be added in the array.
 If axis is provided, then values to be added must be of similar shape as array arr along the axis where we want to add.
 axis: int, optional
 The axis along which values will be added to array. Default value is None.
 If axis is None: Then values array will be flattened and added to the array arr.
 If axis is 0, then values will be appended row wise.
 If axis is 1, then values will be appended column wise.
 The axis along which values will be added to array. Default value is None.
Returns:
 A copy of the given array arr, with values appended to the array.
It doesn’t modify the original array in parameter arr. It creates a copy of this array and appends the elements from values parameter, to the end of this new copied array.Â So, basically it returns a copy of numpy array provided with values appended to it.
Let’s understand by examples :
Append elements at the end of 1D numpy array
Let’s create a Numpy array i.e.
import numpy as np # create a Numpy array from a list arr = np.array([1, 2, 3, 4, 5, 6, 7])
Append a single element to the Numpy array
# Append a single element at the end of Numpy Array newArr = np.append(arr, 88)
Contents of the new Numpy Array returned :
[ 1 2 3 4 5 6 7 88]
Now let’s see how append multiple elements to a Numpy array.
Append elements from a list to the Numpy array
import numpy as np # create a Numpy array from a list arr = np.array([1, 2, 3, 4, 5, 6, 7]) # Append multiple elements from a list to the end of a Numpy Array newArr = np.append(arr, [88,99,100])
Contents of the new Numpy Array returned :
[ 1 2 3 4 5 6 7 88 99 100]
Flatten 2D Numpy Array and add items to it
Let’s create a 2D numpy array i.e.
import numpy as np # Create a 2D Numpy Array like Matrix matrixArr = np.array( [ [1, 2, 3], [ 4, 5, 6] ])
Now append 1D list to this 2D Numpy array.
# Add elements in List to 2D Numpy array by flattening newArr = np.append(matrixArr, [22, 23, 24])
As axis parameter is not provided in call to append(), so both the arrays will be flattened first and then values will appended. Therefore, contents of the new flattened Numpy Array returned are,
[ 1 2 3 4 5 6 22 23 24]
Add a Numpy Array to another array row wise
If we provide axis parameter in append() call then both the arrays should be of same shape. Let’s create two 2D numpy arrays,
import numpy as np # Create two 2D Numpy Array like Matrix matrixArr1 = np.array([[1, 2, 3], [4, 5, 6]]) matrixArr2 = np.array([[70, 80, 90], [61, 62, 63]])
Now let’s append the rows from a numpy array to the end of another numpy array by passing axis as 0 i.e.
newArr = np.append(matrixArr1, matrixArr2 , axis=1)
Contents of 2D array matrixArr2 will be appended to the contents of matrixArr1 as rows in new array. Contents of the returned array are,
[[1 2 3] [4 5 6] [1 2 3] [4 5 6]]
Add a NumPy Array to another array – Column Wise
In the above example if instead of passing axis as 0 we pass axis=1 then contents of 2D array matrixArr2 will be appended to the contents of matrixArr1 as columns in new array i.e.
import numpy as np # Create two 2D Numpy Array like Matrix matrixArr1 = np.array([[1, 2, 3], [4, 5, 6]]) matrixArr2 = np.array([[70, 80, 90], [61, 62, 63]]) newArr = np.append(matrixArr1, matrixArr2 , axis=1)
Contents of the new Numpy Array returned are,
[[ 1 2 3 70 80 90] [ 4 5 6 61 62 63]]
Error while appending elements in Numpy array of different shapes
If you are providing axis parameter in numpy.append() then both the arrays should be of same shape along the given axis, otherwise it will raise Error. For example,
Let’s try to append a 1D array to 2D array with axis = 1 i.e.
import numpy as np # Create a 2D Numpy Array like Matrix matrixArr = np.array( [ [1, 2, 3], [ 4, 5, 6] ]) arr5 = np.append(matrixArr, [22, 23, 24], axis=1 )
It will give following error,
ValueError: all the input arrays must have same number of dimensions
We are trying to row wise append 1D array to a 2D array of shape 2X3, shapes are not compatible therefore, it gave the error. We should make the shape NX3 where N can be anything greater than 1.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.