Numpy arrays are one of the most efficient data structures for numerical data. You can perform different mathematical operations on numpy arrays using built-in functions. This article will discuss how to concatenate numpy arrays in Python using different functions.
- The Concatenate() Function
- Concatenate Numpy arrays using hstack() function in Python
- Concatenate Numpy arrays using vstack() Function in Python
- Stack Arrays using the stack() function
- Stack Numpy Arrays using column_stack() Function in Python
- Stack Numpy Arrays Using row_stack() Function in Python
- Stack Numpy Arrays Across Depth Using dstack() Function
- Conclusion
The Concatenate() Function
You can use concatenate() function to concatenate 1-D and 2-D numpy arrays along an existing axis. It has the following syntax.
np.concatenate((arr1,arr2,…, arrN), axis=0)
Here, the concatenate() function takes a tuple of numpy arrays as its first input argument. After execution, it returns the concatenated array.
- The axis along which the input arrays are concatenated is decided using the axis parameter. It has the default value of 0.
- For axis=0, the rows of the different arrays are concatenated vertically i.e. the rows of different arrays become the rows of the output array.
- For axis=1, the arrays are concatenated horizontally i.e. the columns of the input array become the columns of the output array.
- For axis=None, all the input arrays are flattened and the output is a 1-D numpy array.
Concatenate 1-D arrays using the concatenate() function in Python
You can concatenate 1-D numpy arrays using the concatenate() function by passing a tuple containing the numpy arrays as an input argument as shown below.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,8)
print("Second array is:")
print(arr2)
arr3=np.concatenate([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7]
Concatenated array is:
[0 1 2 3 4 5 6 7]
Here, we have concatenated two numpy arrays horizontally. Hence, all the elements of the input arrays are converted to elements of the output array.
You cannot concatenate 1-D numpy arrays using the concatenate() function vertically using the axis=1 parameter. Doing so will lead to numpy.AxisError exception with the message “numpy.AxisError: axis 1 is out of bounds for array of dimension 1”. You can observe this in the following example.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,8)
print("Second array is:")
print(arr2)
arr3=np.concatenate([arr1,arr2],axis=1)
print("Concatenated array is:")
print(arr3)
Output:
AxisError: axis 1 is out of bounds for array of dimension 1
Here, you can observe that we have tried to concatenate numpy arrays vertically using the concatenate() function that has led to the AxisError exception.
Concatenate 2-D arrays using the concatenate() function in Python
We can concatenate 2-D array horizontally and vertically using the concatenate() function.
To concatenate the numpy arrays horizontally, you can pass a tuple of arrays as the first input argument and axis=1 as the second input argument to the concatenate() function. After execution, the concatenate() function will return a numpy array as shown below.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.concatenate([arr1,arr2],axis=1)
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 1 2 3 4 15 16 17 18 19]
[ 5 6 7 8 9 20 21 22 23 24]
[10 11 12 13 14 25 26 27 28 29]]
In the above example, we have concatenated 2-D numpy arrays horizontally. You can observe that the rows of the input arrays are combined to create the rows of the output array.
While concatenating numpy arrays horizontally, you need to make sure that all the input arrays have the same number of rows. Horizontally concatenating arrays with different numbers of rows will lead to a ValueError exception with the message “ValueError: all the input array dimensions for the concatenation axis must match exactly”. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,35).reshape(4,5)
print("Second array is:")
print(arr2)
arr3=np.concatenate([arr1,arr2],axis=1)
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 3 and the array at index 1 has size 4
In the above example, we have tried to concatenate arrays with 3 and 4 rows. Because the input arrays have different number of rows, the program runs into ValueError exception.
You can also concatenate the numpy arrays vertically using the concatenate() function. For this, you need to pass the parameter axis=0 as input along with the tuple of arrays. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.concatenate([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
In this example, we have concatenated 2-D numpy arrays vertically. You can observe that the columns of the input arrays are combined to create the columns of the output array.
While concatenating numpy arrays vertically, you need to make sure that all the input arrays have the equal number of columns. Otherwise, the program will run into a ValueError exception with the message “ValueError: all the input array dimensions for the concatenation axis must match exactly”. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,36).reshape(3,7)
print("Second array is:")
print(arr2)
arr3=np.concatenate([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 7
In this example, we have tried to concatenate two arrays with 5 and 7 columns respectively. Due to the difference in number of columns, the program runs into ValueError exception while concatenating the arrays.
You can also concatenate all the 2-D numpy arrays to create a 1-D array. For this, you have to pass the parameter axis=None to the concatenate() function along with the tuple of arrays. In this case, all the input arrays are first flattened into 1-D arrays. After that, they are concatenated. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,36).reshape(3,7)
print("Second array is:")
print(arr2)
arr3=np.concatenate([arr1,arr2],axis=None)
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19 20 21]
[22 23 24 25 26 27 28]
[29 30 31 32 33 34 35]]
Concatenated array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35]
In this example, we have created a 1-D array after concatenating 2-D arrays. You can observe that the elements of the input 2-D arrays are included in the output array in row major order.
The concatenate() function combines the input arrays along the existing axis. So, combining 2-D arrays only gives us 2-D arrays as output. If you want to stack numpy arrays to form a data cube-type structure, you cannot do this using the concatenate() function. For this, we will use the stack() function.
Concatenate Numpy arrays using hstack() function in Python
The hstack() function works in a similar manner to the concatenate() function with the parameter axis=1. When we pass a tuple of arrays to the hstack() function, it stacks the columns of the input arrays horizontally and returns a new array. For 1-D input arrays, it returns 1-D array as shown below.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,8)
print("Second array is:")
print(arr2)
arr3=np.hstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7]
Concatenated array is:
[0 1 2 3 4 5 6 7]
Here, you can observe that the input arrays are concatenated to form an output 1-D array. The elements of the input arrays are included in the output array in the same order they are given as input to the hstack() function.
For 2-D array inputs, the hstack() function returns a 2-D array as shown below.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.hstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 1 2 3 4 15 16 17 18 19]
[ 5 6 7 8 9 20 21 22 23 24]
[10 11 12 13 14 25 26 27 28 29]]
In the above example, the rows of the input arrays have been combined to create the rows of the output array.
While using 2-D arrays with the hstack() function, you need to make sure that the input arrays have an equal number of rows. Otherwise, you will get a ValueError exception with the message “ValueError: all the input array dimensions for the concatenation axis must match exactly”. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,35).reshape(4,5)
print("Second array is:")
print(arr2)
arr3=np.hstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 3 and the array at index 1 has size 4
In the above example, we have tried to concatenate two 2-D arrays with different number of rows using the hstack() function. Due to this, the program runs into ValueError exception. So, we can say that the hstack() function works in a similar manner to the concatenate() function with the parameter axis=1.
Concatenate Numpy arrays using vstack() Function in Python
You cannot concatenate numpy arrays vertically using the concatenate() function. However, you can use the vstack() function to concatenate 1-D arrays vertically to create a 2-D numpy array. When we pass a tuple or list of 1-D numpy arrays to the vstack() function, it returns a 2-D array in which all the input numpy arrays are converted into rows. You can observe this in the following example.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,10)
print("Second array is:")
print(arr2)
arr3=np.vstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7 8 9]
Concatenated array is:
[[0 1 2 3 4]
[5 6 7 8 9]]
In this example, we have concatenated two 1-D arrays vertically to create a 2-D array. This isn’t possible using the concatenate() function.
While concatenating 1-D numpy arrays using the vstack() function, you need to make sure that all the arrays have equal lengths. Otherwise, the program will run into a ValueError exception as shown in the following example.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,8)
print("Second array is:")
print(arr2)
arr3=np.vstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 3
Here, you can observe that we have tried to concatenate two arrays of lengths 5 and 3 respectively. Due to this the program runs into the ValueError exception.
You can also concatenate 2-D numpy arrays vertically using the vstack() function. In this case, the vstack() function works in a similar manner to the concatenate() function with the parameter axis=0.
When we pass a list or tuple of 2-D numpy arrays to the vstack() function, we get a 2-D array as output. The rows of the input array are converted to the rows of the output numpy array. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.vstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
In this example, you can observe that we have concatenated two 2-D arrays vertically using the vstack() function. Here, the columns of the input arrays combined to create columns of the output array.
While using 2-D arrays with the vstack() function, you need to make sure that the input arrays have an equal number of columns. Otherwise, you will get a ValueError exception with the message “ValueError: all the input array dimensions for the concatenation axis must match exactly”. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,36).reshape(3,7)
print("Second array is:")
print(arr2)
arr3=np.vstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 7
In this example, we have tried to concatenate two arrays with 5 and 7 columns respectively. Due to this, the program runs into ValueError exception.
For vstack() function, you can observe that it behaves differently from the concatenate() function. In case of 1-D arrays, the concatenate() function works differently from the concatenate() function. However, in case of 2-D arrays, the vstack() function works in a similar manner to the concatenate() function.
Stack Arrays using the stack() function
The stack() function can be used to create an array of dimension N+1 using the arrays of dimension N. For instance, you can create a 3-D array from 2-D arrays or a 2-D array from 1-D arrays using the stack() function.
The stack() function takes a tuple of the input arrays as its first input argument and the axis along which we have to stack the arrays as its second input argument.
Stack 1-D arrays using the stack() function in Python
You can stack a 1-D array across rows and columns using the stack() function. To stack 1-D arrays across rows, you can pass the parameter axis=0 along with the tuple of input arrays as shown below.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,10)
print("Second array is:")
print(arr2)
arr3=np.stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7 8 9]
Concatenated array is:
[[0 1 2 3 4]
[5 6 7 8 9]]
In this example, we have stacked two 1-D arrays vertically to create a 2-D array using the stack() function. Here, the input arrays are converted into rows of the output array.
You can also stack two 1-D arrays across columns using the stack() function. For this, you need to pass the parameter axis=1 along with the tuple of input arrays to the stack() function. In the output, you will get a 2-D array where the input arrays are converted into columns of the output array. You can observe this in the following example.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,10)
print("Second array is:")
print(arr2)
arr3=np.stack([arr1,arr2],axis=1)
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7 8 9]
Concatenated array is:
[[0 5]
[1 6]
[2 7]
[3 8]
[4 9]]
In the above example, you can observe that we have stacked two 1-D numpy arrays to create a 2-D array. Here, the 1-D input arrays are converted into the columns of the 2-D arrays.
You can only stack 1-D numpy arrays of equal length using the stack() function. Otherwise, the program will run into a ValueError exception with the message “ValueError: all input arrays must have the same shape”. You can observe this in the following example.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,15)
print("Second array is:")
print(arr2)
arr3=np.stack([arr1,arr2],axis=1)
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all input arrays must have the same shape
In the above example, we have we have tried to horizontally stack two 1-D numpy arrays. Because the arrays have different lengths, the program runs into ValueError exception saying that all the input arrays must be of the same shape.
Stack 2-D arrays using the stack() function in Python
You can stack 2-D arrays across rows, columns, and depth using the stack() function.
To stack two numpy arrays across the length, we can pass axis=0 to the stack() function along with the tuple of input arrays. If there are K input arrays of shape MxN in the input tuple, the output array will be of shape KxMxN. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.stack([arr1,arr2],axis=0)
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]]
In this example, we have stacked two numpy arrays of shape 3×5 using the stack() function. The resultant array is of the shape 2x3x5. Hence, we are getting 3-D arrays after stacking 2-D arrays using the stack() function.
To stack numpy arrays across the width, you can pass axis=1 to the stack() function along with the tuple of input arrays. If there are K input arrays of shape MxN in the input tuple, the output array will be of shape MxKxN. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.stack([arr1,arr2],axis=1)
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[[ 0 1 2 3 4]
[15 16 17 18 19]]
[[ 5 6 7 8 9]
[20 21 22 23 24]]
[[10 11 12 13 14]
[25 26 27 28 29]]]
In this example, you can observe that we have got the output array of shape 3x2x5. So, the stack() function, when executed with parameter axis=1, stacks the input arrays across the width.
To stack numpy arrays across the depth, we can pass axis=2 to the stack() function along with the tuple of input arrays. If there are K input arrays of shape MxN in the input tuple, the output array will be of shape MxNxK. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.stack([arr1,arr2],axis=2)
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[[ 0 15]
[ 1 16]
[ 2 17]
[ 3 18]
[ 4 19]]
[[ 5 20]
[ 6 21]
[ 7 22]
[ 8 23]
[ 9 24]]
[[10 25]
[11 26]
[12 27]
[13 28]
[14 29]]]
In this example, we have received the output array the shape 3x5x2 after stacking 2 arrays of shape 3×5 across depth.
You should remember that the input arrays passed to the stack() function in the input tuple should be of the same shape. Otherwise, the program will run into a ValueError exception. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,35).reshape(4,5)
print("Second array is:")
print(arr2)
arr3=np.stack([arr1,arr2],axis=2)
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all input arrays must have the same shape
In the above example, we have passed input arrays of different shapes to the stack() function. Due to this, the function raises ValueError exception.
Suggested Reading: If you are into machine learning, you can read this article on regression in machine learning. You might also like this article on k-means clustering with numerical example.
Stack Numpy Arrays using column_stack() Function in Python
The numpy module provides us with the column_stack() function to stack 1-D arrays as columns of a 2-D array. When we pass a list or tuple of 1-D arrays to the column_stack() function, it returns a 2-D array that consists of input 1-D arrays as its columns. You can observe this in the following example.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,10)
print("Second array is:")
print(arr2)
arr3=np.column_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7 8 9]
Concatenated array is:
[[0 5]
[1 6]
[2 7]
[3 8]
[4 9]]
Here, all the input 1-D arrays are stacked as columns of the output array. We can say that the column_stack() function works in a similar manner to the stack() function with axis=1.
The 1-D arrays passed to the column_stack() function must have equal lengths. Otherwise, the program will run into an error.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,12)
print("Second array is:")
print(arr2)
arr3=np.column_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 5 and the array at index 1 has size 7
Here, we have tried to stack two arrays of size 5 and 7 respectively using the column_stack() function. Due to this, the function raises ValueError exception.
You can also stack 2-D arrays horizontally using the column_stack() function as shown below.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.column_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 1 2 3 4 15 16 17 18 19]
[ 5 6 7 8 9 20 21 22 23 24]
[10 11 12 13 14 25 26 27 28 29]]
When we stack 2-D arrays using the column_stack() function, the rows of the input arrays are combined to create the rows of the output arrays. So, the function works in a similar manner to the hstack() function or the vstack() function with axis=1.
When you stack 2-D arrays using the column_stack() function, you need to make sure that the number of rows in each input array is the same. Otherwise, the program will run into a ValueError exception as shown below.
import numpy as np
arr1=np.arange(15).reshape((3,5))
print("First array is:")
print(arr1)
arr2=np.arange(15,35).reshape(4,5)
print("Second array is:")
print(arr2)
arr3=np.column_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 3 and the array at index 1 has size 4
You can also stack 1-D and 2-D arrays as columns of a new array using the column_stack() function as shown below.
import numpy as np
arr1=np.arange(3)
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.column_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 15 16 17 18 19]
[ 1 20 21 22 23 24]
[ 2 25 26 27 28 29]]
Here, you can observe that the 1-D array as well as the columns of the 2-D array constitute the columns of the output array. Again, it is important that the length of the 1-D arrays and the number of rows in the 2-D array should be the same. Otherwise, the program will run into exceptions as shown below.
import numpy as np
arr1=np.arange(4)
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.column_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 4 and the array at index 1 has size 3
Stack Numpy Arrays Using row_stack() Function in Python
The numpy module provides us with the row_stack() function to stack 1-D arrays as rows of a 2-D array. When we pass a list or tuple of 1-D arrays to the row_stack() function, it returns a 2-D array that consists of input 1-D arrays as its rows. You can observe this in the following example.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,10)
print("Second array is:")
print(arr2)
arr3=np.row_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7 8 9]
Concatenated array is:
[[0 1 2 3 4]
[5 6 7 8 9]]
In the above example, we have stacked two 1-D arrays of length 5 to create a 2-D array of size 2×5. By observing the output, we can say that the row_stack() function works in a similar manner to the vstack() function.
The 1-D arrays passed to the row_stack() function must have equal lengths. Otherwise, the program will run into an error.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,11)
print("Second array is:")
print(arr2)
arr3=np.row_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 6
You can also stack 2-D arrays vertically using the row_stack() function as shown below.
import numpy as np
arr1=np.arange(15).reshape(3,5)
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.row_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
In this example, we have stacked two 2-D arrays vertically using the row_stack() function. You can observe that the columns of the input arrays have been combined to create the columns of the output array.
When you stack 2-D arrays using the row_stack() function, you need to make sure that the number of columns in each input array is the same. Otherwise, the program will run into a ValueError exception as shown below.
import numpy as np
arr1=np.arange(15).reshape(3,5)
print("First array is:")
print(arr1)
arr2=np.arange(15,33).reshape(3,6)
print("Second array is:")
print(arr2)
arr3=np.row_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 6
You can also stack 1-D and 2-D arrays as rows of a new array using the row_stack() function as shown below.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.row_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[ 0 1 2 3 4]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Here, you can observe that the 1-D array as well as the columns of the 2-D array constitute the rows of the output array. Again, it is important that the length of the 1-D arrays and the number of columns in the input 2-D array should be the same. Otherwise, the program will run into exceptions as shown below.
import numpy as np
arr1=np.arange(6)
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.row_stack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 6 and the array at index 1 has size 5
Stack Numpy Arrays Across Depth Using dstack() Function
The dstack() function is used to stack numpy arrays across depth. When we pass a list or tuple of 1-D arrays to the dstack() function, it returns a 3-D array as shown below.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,10)
print("Second array is:")
print(arr2)
arr3=np.arange(11,16)
print("Third array is:")
print(arr3)
arr4=np.dstack([arr1,arr2,arr3])
print("Concatenated array is:")
print(arr4)
Output:
First array is:
[0 1 2 3 4]
Second array is:
[5 6 7 8 9]
Third array is:
[11 12 13 14 15]
Concatenated array is:
[[[ 0 5 11]
[ 1 6 12]
[ 2 7 13]
[ 3 8 14]
[ 4 9 15]]]
If K 1-D arrays of length N are given as input to the dstack() function, the output array is of the shape 1xNxK.
The length of the 1-D arrays given as input to the dstack() function should be the same. If the length of input arrays are different, the program runs into a ValueError exception as shown below.
import numpy as np
arr1=np.arange(5)
print("First array is:")
print(arr1)
arr2=np.arange(5,11)
print("Second array is:")
print(arr2)
arr3=np.arange(11,16)
print("Third array is:")
print(arr3)
arr4=np.dstack([arr1,arr2,arr3])
print("Concatenated array is:")
print(arr4)
Output:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 6
For 2-D arrays, the dstack() function works in a similar manner to the stack() function with the parameter axis=2. If there are K input arrays of shape MxN in the input tuple, the output array will be of shape MxNxK. You can observe this in the following example.
import numpy as np
arr1=np.arange(15).reshape(3,5)
print("First array is:")
print(arr1)
arr2=np.arange(15,30).reshape(3,5)
print("Second array is:")
print(arr2)
arr3=np.dstack([arr1,arr2])
print("Concatenated array is:")
print(arr3)
Output:
First array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Second array is:
[[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
Concatenated array is:
[[[ 0 15]
[ 1 16]
[ 2 17]
[ 3 18]
[ 4 19]]
[[ 5 20]
[ 6 21]
[ 7 22]
[ 8 23]
[ 9 24]]
[[10 25]
[11 26]
[12 27]
[13 28]
[14 29]]]
In this example, we have stacked two 2-D arrays of shape 3×5 across depth. The output array is of the shape 3x5x2. Hence, we can say that the dstack() function works as the stack() function with the parameter axis=2.
Conclusion
In this article, we have discussed how to concatenate 1-dimensional and 2-dimensional NumPy arrays in Python. We also discussed how to stack 1-D and 2-D numpy arrays horizontally, vertically, and across depth.
To learn more about dataframes and numpy arrays, you can read this article on pandas dataframe index. You might also like this article on text analysis in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.