0

I am new to python and numpy. I am trying to convert a 1D array into a column , I used transpose, but not working, please any help, I attached the code:

import numpy as np
x=np.array([1,2,3])
print(x)
y=x.T
print(y)
#output should be:
#y=[[1],
 #   [2],
#    [3]]
2
  • Did you try np.transpose ? Commented Oct 10, 2021 at 11:32
  • Use y = x[:, None] Commented Oct 10, 2021 at 11:33

1 Answer 1

1

You can use transpose for 2d array and you can see difference for your output you can use .reshape(-1,1) like below:

>>> x.reshape(-1,1)
array([[1],
       [2],
       [3]])

Or You can read with more detail in this thread and try this:

>>> np.array([x]).T

>>> np.transpose([x])
Sign up to request clarification or add additional context in comments.

3 Comments

and Dani Mesejo, Thank you for your answers, it works, and I accept your answer, thank you so much ,
I added another question for repeating, hope you could have a look, and help me, Thank you so much
@coder_beginner definitely I take look

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.