How to convert 1-D arrays as columns into a 2-D array in Python?

Last Updated : 13 Jan, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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
# 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)

 
 

Output:


 

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


 

Example 2:


 

Python3
# 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)

 
 

Output:


 

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


 


Next Article
Practice Tags :

Similar Reads