Concatenate NumPy Arrays
Concatenation combines arrays along an existing axis when their other dimensions are compatible.
What is Concatenate NumPy Arrays?
Concatenation combines arrays along an existing axis when their other dimensions are compatible.
Join NumPy arrays by row or column.
When should you use it?
- Perform numerical operations on many values at once.
- Process arrays, matrices, and scientific data efficiently.
- Prepare data for data science and machine learning.
Example code
main.py
import numpy as np
first = np.array([[1, 2], [3, 4]])
second = np.array([[5, 6]])
print(np.concatenate((first, second), axis=0))
Expected output
[[1 2]
[3 4]
[5 6]]
How it works
Axis zero adds rows. Both input arrays have the same number of columns, so they can be stacked vertically.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input shape and dtype.
- Try negative, floating-point, and missing values.
- Apply the operation to larger multidimensional arrays and measure performance.