w3resource

NumPy: Stack arrays in sequence vertically

NumPy: Array Object Exercise-128 with Solution

Write a NumPy program to stack arrays in sequence vertically.

Pictorial Presentation:

NumPy: Stack arrays in sequence vertically

Sample Solution:

Python Code:

import numpy as np    
print("\nOriginal arrays:")
x = np.arange(9).reshape(3,3)
y = x*3
print("Array-1")
print(x)
print("Array-2")
print(y)
new_array =  np.vstack((x,y))
print("\nStack arrays in sequence vertically:")
print(new_array)

Sample Output:

Original arrays:
Array-1
[[0 1 2]
 [3 4 5]
 [6 7 8]]
Array-2
[[ 0  3  6]
 [ 9 12 15]
 [18 21 24]]

Stack arrays in sequence vertically:
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 0  3  6]
 [ 9 12 15]
 [18 21 24]]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to stack arrays in sequence horizontally (column wise).
Next: Write a NumPy program to stack 1-D arrays as columns wise.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}