0

I have data like below.

[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34.] <br>

(34,)

But I want to make this (1,34).
When I use np.reshape like

np.reshape(data, (1,34))

I get data like below.

[[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
  19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34.]]

(1, 34)

But it looks weird for me. Could you recommend a solution?
%np.transpose does not change anything.

DD DD
  • 1,148
  • 1
  • 10
  • 33
  • 1
    It is not clear to me what exactly you are asking. You should elaborate with better examples. – Leonid Mar 19 '19 at 02:17
  • From what I understand, you want the rows to become columns, and the columns to become rows. Assuming your data is stored in `data`, try using `data.T`. Like @Leonid said, your question is not very clear. – hridayns Mar 19 '19 at 02:23
  • 1
    *"But it looks weird for me."* It looks correct to me, and it is what you say you wanted. – Warren Weckesser Mar 19 '19 at 02:23
  • @hridayns, `data` is a one-dimensional array with shape (34,). In numpy, the transpose operation *swaps* existing dimensions; it doesn't create new dimensions. So on a one-dimensional array, the transpose operation doesn't do anything. – Warren Weckesser Mar 19 '19 at 02:25
  • A (1,34) shape array displays like a list with 1 element - that element is itself a list - a list with 34 elements. That's what the weird display shows, right? By convention we think of the first dimension of a 2d array, as the number of rows, and the second as the number columns. A 1d array just has elements (no rows or columns). – hpaulj Mar 19 '19 at 02:30
  • The question title is *"How to convert row to column in Python"*, but you say *"But I want to make this (1,34)."* If you want a *column*, the desired shape is (34, 1). – Warren Weckesser Mar 19 '19 at 02:30
  • @WarrenWeckesser Ah yes, I actually was looking at the data after he reshaped it. My bad. – hridayns Mar 19 '19 at 02:37

2 Answers2

0

So with numpy's reshape(), it will change the dimensions of the array. And those dimensions must be factors of the sum of elements. In your case your arrary has 34 values, so you only have 4 options. Either you have a dimension of (1,34) or (2,17), which look like:

    array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
        33, 34]])

and

array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17],
       [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
        34]])

Or you can do the reverse of the above with (34, 1):

array([[ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
         .
         .
       [34]

or finally (17, 2) which looks like :

array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10],
       [11, 12],
       [13, 14],
       [15, 16],
       [17, 18],
       [19, 20],
       [21, 22],
       [23, 24],
       [25, 26],
       [27, 28],
       [29, 30],
       [31, 32],
       [33, 34]])

This is because 34 only breaks down to 2 * 17 or 17 * 2.

RockAndRoleCoder
  • 320
  • 2
  • 10
0

You can also use the transpose of the array to (df.T) to get the desired output.