2

I am trying to extract a column and arrange into multiple rows. My Input: data

-2.74889,1.585,223.60

-2.74889,1.553,228.60

-2.74889,1.423,246.00

-2.74889,1.236,249.10

-2.74889,0.928,243.80

-2.74889,0.710,242.20

-2.74889,0.558,243.50
...
...
...

k = np.reshape(data[:,2], (2,10))

Output:

[[ 223.6   228.6   246.    249.1   243.8   242.2   243.5   244.    244.8
   245.2 ]

 [ 224.6   230.    250.7   249.3   244.4   242.1   242.8   243.8   244.7
   245.1 ]]

My question is how to add square brackets for each number(for example 223.6) and remain them in 1 row?

Thanks, Prasad.

0

2 Answers 2

1

It's not entirely clear what you mean, but perhaps it's something like this?

>>> import numpy as np
>>> data = np.arange(30).reshape(10,3)
>>> data
array([[ 0,  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]])
>>> data[:, 2, None]
array([[ 2],
       [ 5],
       [ 8],
       [11],
       [14],
       [17],
       [20],
       [23],
       [26],
       [29]])
Sign up to request clarification or add additional context in comments.

Comments

0

You need to expand the dimensions of the array when you reshape.

Setup

x = np.arange(60).reshape(20, 3)

reshape with an additional dimension

x[:, 2].reshape((-1, 10, 1))

expand_dims with axis=2

np.expand_dims(x[:, 2].reshape(-1, 10), axis=2)

atleast_3d

np.atleast_3d(x[:, 2].reshape(-1, 10))

All three produce:

array([[[ 2],
        [ 5],
        [ 8],
        [11],
        [14],
        [17],
        [20],
        [23],
        [26],
        [29]],

       [[32],
        [35],
        [38],
        [41],
        [44],
        [47],
        [50],
        [53],
        [56],
        [59]]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.