- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Join a sequence of Numpy arrays with stack() over axis 1

To join a sequence of arrays, use the **numpy.stack()** method in Python Numpy. The axis parameter specifies the index of the new axis in the dimensions of the result. Here, we have set axis 1.

The function returns the stacked array has one more dimension than the input arrays. The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

The out parameter, if provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified.

## Steps

At first, import the required library −

import numpy as np

Creating two numpy arrays using the array() method. We have inserted elements of int type −

arr1 = np.array([49, 76, 61, 82, 69, 29]) arr2 = np.array([40, 60, 89, 55, 32, 98])

Display the arrays −

print("Array 1...

", arr1) print("

Array 2...

", arr2)

Get the type of the arrays −

print("

Our Array 1 type...

", arr1.dtype) print("

Our Array 2 type...

", arr2.dtype)

Get the dimensions of the Arrays −

print("

Our Array 1 Dimensions...

",arr1.ndim) print("

Our Array 2 Dimensions...

",arr2.ndim)

Get the shape of the Arrays −

print("

Our Array 1 Shape...

",arr1.shape) print("

Our Array 2 Shape...

",arr2.shape)

To join a sequence of arrays, use the numpy.stack() method. The axis parameter specifies the index of the new axis in the dimensions of the result. If axis=0 it will be the first dimension and if axis=-1 it will be the last dimension −

print("

Result (stack over axis 1)...

",np.stack((arr1, arr2), axis = 1))

## Example

import numpy as np # Creating two numpy arrays using the array() method # We have inserted elements of int type arr1 = np.array([49, 76, 61, 82, 69, 29]) arr2 = np.array([40, 60, 89, 55, 32, 98]) # Display the arrays print("Array 1...

", arr1) print("

Array 2...

", arr2) # Get the type of the arrays print("

Our Array 1 type...

", arr1.dtype) print("

Our Array 2 type...

", arr2.dtype) # Get the dimensions of the Arrays print("

Our Array 1 Dimensions...

",arr1.ndim) print("

Our Array 2 Dimensions...

",arr2.ndim) # Get the shape of the Arrays print("

Our Array 1 Shape...

",arr1.shape) print("

Our Array 2 Shape...

",arr2.shape) # To join a sequence of arrays, use the numpy.stack() method in Python Numpy # The axis parameter specifies the index of the new axis in the dimensions of the result. # If axis=0 it will be the first dimension and if axis=-1 it will be the last dimension. print("

Result (stack over axis 1)...

",np.stack((arr1, arr2), axis = 1))

## Output

Array 1... [49 76 61 82 69 29] Array 2... [40 60 89 55 32 98] Our Array 1 type... int64 Our Array 2 type... int64 Our Array 1 Dimensions... 1 Our Array 2 Dimensions... 1 Our Array 1 Shape... (6,) Our Array 2 Shape... (6,) Result (stack over axis 1)... [[49 40] [76 60] [61 89] [82 55] [69 32] [29 98]]

- Related Questions & Answers
- Join a sequence of Numpy arrays with stack() over axis 0
- Join a sequence of arrays with stack() over specific axis in Numpy
- Join a sequence of arrays with stack() over negative axis in Numpy
- Join a sequence of Numpy arrays with stack()
- Join a sequence of masked arrays along axis 1 in Numpy
- Join a sequence of masked arrays along axis 0 in Numpy
- Join a sequence of masked arrays along negative axis in Numpy
- Join a sequence of masked arrays in Numpy
- Stack masked arrays in sequence depth wise (along third axis) in Numpy
- Concatenate a sequence of masked arrays along specific axis in Numpy
- Concatenate a sequence of masked arrays along axis 0 in Numpy
- Stack arrays in sequence vertically (row wise) in Numpy
- Append masked arrays along axis 1 in Numpy
- Stack masked arrays in sequence horizontally (column wise) in Numpy
- Stack masked arrays in sequence vertically (row wise) in Numpy