w3resource

NumPy: Stack 1-D arrays as columns wise

NumPy: Array Object Exercise-129 with Solution

Write a NumPy program to stack 1-D arrays as columns wise.

Sample Solution:

Python Code:

import numpy as np    
print("\nOriginal arrays:")
x = np.array((1,2,3))
y = np.array((2,3,4))
print("Array-1")
print(x)
print("Array-2")
print(y)
new_array =  np.column_stack((x, y))
print("\nStack 1-D arrays as columns wise:")
print(new_array)

Sample Output:

Original arrays:
Array-1
[1 2 3]
Array-2
[2 3 4]

Stack 1-D arrays as columns wise:
[[1 2]
 [2 3]
 [3 4]]

Pictorial Presentation:

Python NumPy: Stack 1-D arrays as columns wise.

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 vertically.
Next: Write a NumPy program to stack 1-D arrays as row wise.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

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