Skip to content
Related Articles

Related Articles

How to convert 1-D arrays as columns into a 2-D array in Python?
  • Last Updated : 13 Jan, 2021

Let’s see a program to convert 1-D arrays as columns into a 2-D array using NumPy library in Python. So, for solving this we are using numpy.column_stack() function of NumPy. This function takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array.

Syntax : numpy.column_stack(tuple)

Parameters :

tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same first dimension.

Return : [stacked 2-D array] The stacked 2-D array of the input arrays.



Now, let’s see an example:

 Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import numpy as np
 
# create a 1d-array
a = np.array(("Geeks", "for",
              "geeks"))
 
# create a 1d-array
b = np.array(("my", "name",
              "sachin"))
 
# convert 1d-arrays into
# columns of 2d-array
c = np.column_stack((a, b))
 
print(c)

chevron_right


 
 

Output:

 

[['Geeks' 'my']
 ['for' 'name']
 ['geeks' 'sachin']]

 

Example 2:



 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import numpy as np
 
# create 1d-array
a = np.array((1,2,3,4))
 
# create 1d-array
b = np.array((5,6,7,8))
 
# convert 1d-arrays into
# columns of 2d-array
c = np.column_stack((a, b)) 
 
print(c)

chevron_right


 
 

Output:

 

[[1 5]
[2 6]
[3 7]
[4 8]]

 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :