numpy.vstack() in Python

The Python numpy.vstack() function used to stack the sequence of input arrays vertically to make a single array and returns at least 2D array formed by stacking given array.

Basic Syntax

Following is the basic syntax for numpy.vstack() function in Python:

numpy.vstack(tup)

And the parameters are:

Parameter Description
tup [SQUESNCE OF NDARRAY]. Tuples holding arrays to be stacked.

Return Value

This function returns at least 2D array created by stacking given array.

Example

Following are the examples:

Example 1

# Python program to illusrate vstack() function 

import numpy as np 

# Input arrays to be stacked 
first_array = np.array([ 2, 3, 4, 8] ) 
print ("First input array: \n", first_array) 

second_array = np.array([ 10, 12, 14, 16] ) 
print ("Second input array: \n", second_array) 

# Stacking the two arrays vertically
result_array = np.vstack((first_array, second_array)) 
print ("Vertically stacked array:\n ", result_array) 

The output for the above program is as given below:

First input array:
[2, 3, 4, 8]
Second input array:
[10, 12, 14, 16]
Vertically stacked array:
[[2, 3, 4, 8],
[10, 12, 14, 16]]

Example 2

# Python program to illustrate vstack() function 
  
import numpy as np
  
# Input arrays to be stacked 
first_array = np.array([[ 1, 2, 3, 4], [ 5, 6, 7, 8]] ) 
print ("First Input array : \n", first_array)  
  
second_array = np.array([[ 9, 10, 11, 12], [ 13, 14, 15, 16]] ) 
print ("Second Input array : \n", second_array)  
  
# result of Vertically stacked arrays
result_array = np.vstack((first_array, second_array)) 
print ("Vertically stacked array :\n ", result_array) 

The output for the above program is as given below:

First Input array :
[[1 2 3 4]
[5 6 7 8]]
Second Input array :
[[ 9 10 11 12]
[13 14 15 16]]
Vertically stacked array :
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]