Do you want to know what is the difference between NumPy concatenate vs append in Python? In this NumPy Python article, I will explain what the NumPy concatenate is and what the NumPy append is with illustrative examples. Also, I have created a table of the key difference between NumPy concatenate vs append in Python.
The main difference between np.concatenate and np.append in Python NumPy is that np.concatenate is primarily used for concatenating multiple arrays along specified axes, allowing for more complex array manipulation, while np.append is designed for appending elements to the end of an existing 1D NumPy array.
np.concatenate in Python
The np.concatenate function in NumPy is designed for concatenating arrays along specified axes in Python. It is a versatile tool for combining arrays of the same shape and is particularly useful when we need to join multiple arrays along a specific axis, such as rows (axis 0) or columns (axis 1).
Here’s the basic syntax of np.concatenate() function in Python:
np.concatenate((arr1, arr2, ...), axis=0)
|arr1, arr2, and so on||They are the arrays we want to concatenate in Python.|
|axis||This specifies the axis along which the concatenation should occur. By default, it concatenates along axis 0 (rows).|
Let’s look at a few examples to understand how np.concatenate in Python works:
Example 1: NumPy concatenate along rows(default axis = 0) in Python
In this instance, we have two 2D arrays in Python. and we are concatenating one of the arrays to the other and creating a new NumPy Python array.
import numpy as np state1_population = np.array([[1000000, 500000], [300000, 200000]]) state2_population = np.array([[750000, 400000]]) usa_population = np.concatenate((state1_population, state2_population)) print(usa_population)
Output: The implementation of the code in the Python Pycharm editor
[[1000000 500000] [ 300000 200000] [ 750000 400000]]
Example 2: NumPy concatenate multiple arrays in Python along columns (axis = 1)
Let’s try to concatenate two NumPy arrays in Python.
import numpy as np state1_gdp = np.array([[50000, 55000, 60000]]) state2_gdp = np.array([[63000, 64000, 68000]]) usa_gdp = np.concatenate((state1_gdp, state2_gdp), axis=1) print(usa_gdp)
Output: The implementation of the code with a screenshot in Python.
[[50000 55000 60000 63000 64000 68000]]
np.append in Python
On the other hand, np.append is a simpler function that appends values to an existing NumPy array along a specified axis in Python. While it can be used for concatenation, it is more suitable for adding individual elements or arrays to the end of an existing array in Python.
Here’s the syntax of the numpy.append() function in Python:
np.append(arr, values, axis=None)
|arr||The input NumPy array in Python.|
|values||The values to append to arr through Python.|
|axis||(optional) The axis along which to append the values. If not specified, the array is flattened before appending.|
Here are some examples to illustrate what the np.append does:
Example 1: NumPy append two 1d arrays in Python
Let’s take two arrays and try to append the value of one numpy array to another’s end through Python.
import numpy as np temperatures = np.array([72, 74, 71, 68, 75, 76, 70]) additional_temperature = np.array() updated_temperatures = np.append(temperatures, additional_temperature) print(updated_temperatures)
Output: The implementation of the Python code:
[72 74 71 68 75 76 70 73]
Example 2: Python append multiple arrays
Here, Let’s take two arrays and append the values from one NumPy array to the another through Python np.append() function.
import numpy as np top_universities = np.array([["Harvard", "MIT", "Stanford"]]) new_university = np.array([["Yale", "Caltech", "Princeton"]]) updated_universities = np.append(top_universities, new_university, axis=0) print(updated_universities)
Output: The implementation of the code in Python:
[['Harvard' 'MIT' 'Stanford'] ['Yale' 'Caltech' 'Princeton']]
NumPy concatenate vs append in Python
Now that we have seen examples of both np.concatenate and np.append, let’s summarize the key differences:
|Operation||Concatenates two or more NumPy arrays along the specified axis in Python.||Appends values to the end of a NumPy array in Python.|
|Input Arrays||Expects a sequence of arrays to be concatenated.||Expect two arrays as input.|
|Axis Parameter||Allows specifying the axis along which to concatenate.||No direct control over the axis.|
|Usage for Arrays||Commonly used for concatenating NumPy arrays along an axis.||Generally used for appending elements.|
|Performance||Typically more efficient for concatenating multiple arrays along an axis in Python.||May be less efficient when appending to the end of an array multiple times in Python.|
|In-Place Option||Can be used in-place by assigning the result to an existing Python NumPy array.||Does not provide the in-place option.|
|Multiple Axes||Supports concatenation along multiple axes simultaneously.||Only appends elements to the end of a Python NumPy array.|
|Flexibility||Offers more control and flexibility in concatenation operations.||Simpler but less versatile for array manipulation.|
|Example||Python np.concatenate([arr1, arr2], axis=0)||Python np.append(arr1, arr2)|
These are the key differences between NumPy concatenate vs append in Python.
np.concatenate vs np.append in coding
Here, I have taken one example for each np.concatenate() and np.append() function simultaneously to make you understand easily:
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6]]) result = np.concatenate([arr1, arr2], axis=0) print('Result for concatenate:\n', result)
import numpy as np arr1 = np.array([1, 2, 3]) element1 = 4 element2 = 5 result = np.append(arr1, [element1, element2]) print('Result for append:\n', result)
The Outputs are:
Result for concatenate: [[1 2] [3 4] [5 6]]
Result for append: [1 2 3 4 5]
Understanding what is the difference between NumPy concatenate vs append in Python? The np.concatenate is suitable for more complex array concatenation operations, while np.append is designed for simpler tasks like adding elements to the end of an array. The choice between them depends on the specific task one needs to perform.
You may also like to read:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.