Don Smythe
Don Smythe

Reputation: 9804

How to reformat a 2d numpy array

I have a numpy array such as this:

x = np.arange(0,9)
y = np.arange(20,29)
X = np.array([x, y])

so X looks like [[0,1,2,...9],[20,21,...,29]]

but I would like X to be shaped like this:

X = np.array([[0, 20],
          [1, 21],
          [2, 22],
          ...
          [9, 29]])

How can I do this with x, and y arrays given above?

Upvotes: 0

Views: 103

Answers (2)

Daniel
Daniel

Reputation: 42748

Transpose the array:

x = np.arange(0,10)
y = np.arange(20,30)
X = np.vstack([x, y]).T

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

you can transpose X to get desired result:

In [16]: X
Out[16]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
       [20, 21, 22, 23, 24, 25, 26, 27, 28]])

In [17]: X.T
Out[17]:
array([[ 0, 20],
       [ 1, 21],
       [ 2, 22],
       [ 3, 23],
       [ 4, 24],
       [ 5, 25],
       [ 6, 26],
       [ 7, 27],
       [ 8, 28]])

Upvotes: 1

Related Questions