GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — NumPy: Convert 1-D arrays as columns into a 2-D array - w3resource

NumPy: Convert 1-D arrays as columns into a 2-D array

Last update on August 29 2025 12:46:40 (UTC/GMT +8 hours)


Convert 1D Arrays to 2D (as Columns)

Write a NumPy program to convert 1-D arrays as columns into a 2-D array.
Sample array: (10,20,30), (40,50,60)

Pictorial Presentation:

Python NumPy: Convert 1-D arrays as columns into a 2-D array

Sample Solution:

Python Code:

# Importing the NumPy library with an alias 'np'
import numpy as np

# Creating NumPy arrays 'a' and 'b'
a = np.array((10, 20, 30))
b = np.array((40, 50, 60))

# Stacking arrays 'a' and 'b' as columns using np.column_stack
c = np.column_stack((a, b))

# Printing the resulting array 'c'
print(c) 

Sample Output:

[[10 40]                                                               
 [20 50]                                                               
 [30 60]] 

Explanation:

‘a = np.array((10,20,30))’ creates a 1D array a with shape (3,).

‘b = np.array((40,50,60))’ creates another 1D array b with shape (3,).

c = np.column_stack((a, b)): The np.column_stack() function is used to stack the two arrays ‘a’ and ‘b’ as columns in a new 2D array. The resulting array ‘c’ has the shape (3, 2), where the first column contains the elements of ‘a’ and the second column contains the elements of ‘b’.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to convert two 1D arrays into a single 2D array where each array forms a column using np.column_stack.
  • Stack two 1D arrays column-wise and verify the output shape matches the expected dimensions.
  • Implement a function that reshapes a 1D array into a 2D column vector using np.reshape.
  • Use np.newaxis to convert a 1D array into a 2D column array and compare it with the output of np.atleast_2d.

Go to:

  • NumPy Array Exercises Home ↩
  • NumPy Exercises Home ↩

PREV : Concatenate 2D Arrays
NEXT : Depth-Wise Conversion of 1D to 2D


Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.