. Advertisement .
..3..
. Advertisement .
..4..
Several steps are needed to concatenate arrays in Python NumPy, but all of them are pretty simple. If you have no idea how to tackle this issue, check this article for more.
Methods to Concatenate Arrays in Python NumPy
Method 1. Concatenate 1-Dimensional Arrays in NumPy
Suppose you wish to concatenate one 1-dimensional array in NumPy with another 1-D array. As you join these two 1-D arrays, their elements are directly concatenated.
This simple example can show you how that works:
# Concatenating 1-dimensional NumPy Arrays
import numpy as np
array1 = np.array([0,1,2,3])
array2 = np.array([4,5,6,7])
joined = np.concatenate((array1, array2))
print(joined)
# Returns: [0 1 2 3 4 5 6 7]
You may see that, when we passed in these two arrays, they became merged in the same order we listed them. But what if you decided to switch that order in your concatenate()
functions? Then the arrays would send back the line [4 5 6 7 0 1 2 3].
Method 2. Concatenate 1-Dimensional Arrays in NumPy Row-Wise
So, let’s say you like to join the arrays row-wise. What will be the best method?
We suggest you count on the vstack()
functions because concatenate()
cannot operate along row-axises. On the other hand, vstack()
enables you to do so and sends back two-dimensional arrays.
This example might clear things up for you:
# Concatenating 1-dimensional NumPy Arrays
import numpy as np
array1 = np.array([0,1,2,3])
array2 = np.array([4,5,6,7])
joined = np.vstack((array1, array2))
print(joined)
# Returns:
# [[0 1 2 3]
# [4 5 6 7]]
As you can see, what we get are two-dimensional arrays.
Method 3. Concatenate 2-Dimensional Arrays In NumPy Row-Wise
How about joining 2D arrays row-wise? The concatenate()
command is also useful here. When we say “row-wise”, it means each of your arrays is inserted as if it were one row. When you do so, you will concatenate along the 0th axis.
In these cases, concatenate()
assumes all arrays are of similar dimensions. To understand this concept better, refer to the illustration below:
# Concatenating 2-dimensional NumPy Arrays Row-Wise
import numpy as np
array1 = np.array([[0,1,2], [3,4,5]])
array2 = np.array([[10,21,31], [43,54,65]])
joined = np.concatenate((array1, array2))
print(joined)
# Returns:
# [[ 0 1 2]
# [ 3 4 5]
# [10 21 32]
# [43 54 65]]
Method 4. Concatenate 2-Dimensional Arrays in NumPy Column-Wise
To join arrays in column-wise orders, use concatenate()
functions again. But this time, we will choose the parameter “axis=1” to specify clearly that we need to concatenate our arrays along a column axis.
Here is the example:
# Concatenating 2-dimensional NumPy Arrays Column-Wise
import numpy as np
array1 = np.array([[0,1,2], [3,4,5]])
array2 = np.array([[10,21,32], [43,54,65]])
joined = np.concatenate((array1, array2), axis=1)
print(joined)
# Returns:
# [[ 0 1 2 10 21 32]
# [ 3 4 5 43 54 65]]
Conclusion
This article has shown you how to concatenate arrays in Python NumPy. Clear examples accompany each method to help you understand the concepts better. Feel free to browse this guideline and other tutorials for similar issues regarding Python NumPy arrays (such as converting arrays into DataFrames).
Leave a comment