← Back to NumPy Course | Chapter 5: Array Manipulation | Lesson 5 of 7

vstack/hstack

vstack stacks arrays on top of each other and hstack places them side by side.

In this page:

  1. vstack/hstack
Syntax
python
np.vstack((arr1, arr2))
np.hstack((arr1, arr2))

vstack/hstack

vstack joins vertically (more rows) and hstack joins horizontally (more columns). They are convenient shorthands over concatenate that also handle 1-D inputs sensibly. np.column_stack turns 1-D arrays into columns.

Note: vstack of two 1-D arrays gives a 2-row matrix.

Example: vstack/hstack

python
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))
print(np.hstack((a, b)))

# Output:
# [[1 2 3]
#  [4 5 6]]
# [1 2 3 4 5 6]
Related Topics
Common Mistakes
  1. Using hstack when you meant vstack
  2. Passing mismatched lengths
  3. Forgetting to pass a tuple or list
Chapter Summary
  • vstack adds rows
  • hstack adds columns
  • Both take a tuple of arrays
  • Handy for 1-D inputs
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.