Stacking is similar to concatenation, with the main distinction being that stacking occurs along a new axis.
When concatenating two 1-D arrays along the second axis, the result is that they are stacked on top of each other.
To perform stacking, we provide a sequence of arrays to the stack() method along with the specified axis. If the axis is not explicitly defined, it defaults to 0.
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.stack((arr1, arr2), axis=1) print(arr) |