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
import numpy as np
a = np.array(( "Geeks" , "for" ,
"geeks" ))
b = np.array(( "my" , "name" ,
"sachin" ))
c = np.column_stack((a, b))
print (c)
|
Output:
[['Geeks' 'my']
['for' 'name']
['geeks' 'sachin']]
Example 2:
Python3
import numpy as np
a = np.array(( 1 , 2 , 3 , 4 ))
b = np.array(( 5 , 6 , 7 , 8 ))
c = np.column_stack((a, b))
print (c)
|
Output:
[[1 5]
[2 6]
[3 7]
[4 8]]
Share your thoughts in the comments
Please Login to comment...