3

I have an array as shown below,

[[240.66666667 171.22222222 158.33333333]
 [218.66666667 134.77777778 143.33333333]
 [197.33333333 118.55555556 128.44444444]
 [195.22222222 119.33333333 126.11111111]
 [196.77777778 118.55555556 123.77777778]
 [183.11111111 111.88888889 118.88888889]
 [173.77777778 106.77777778 114.44444444]]

I want to slice the first and third columns for all the rows and wanna get this output,

[[240.66666667 158.33333333]
 [218.66666667 143.33333333]
 [197.33333333 128.44444444]
 [195.22222222 126.11111111]
 [196.77777778 123.77777778]
 [183.11111111 118.88888889]
 [173.77777778 114.44444444]]

Does anyone have any ideas?

Output screenshot:

enter image description here

2 Answers 2

6

You can just give the columns you want like,

>>> data
array([[240.66666667, 171.22222222, 158.33333333],
       [218.66666667, 134.77777778, 143.33333333],
       [197.33333333, 118.55555556, 128.44444444],
       [195.22222222, 119.33333333, 126.11111111],
       [196.77777778, 118.55555556, 123.77777778],
       [183.11111111, 111.88888889, 118.88888889],
       [173.77777778, 106.77777778, 114.44444444]])
>>> data[:, [0,2]]
array([[240.66666667, 158.33333333],
       [218.66666667, 143.33333333],
       [197.33333333, 128.44444444],
       [195.22222222, 126.11111111],
       [196.77777778, 123.77777778],
       [183.11111111, 118.88888889],
       [173.77777778, 114.44444444]])
>>> 
Sign up to request clarification or add additional context in comments.

6 Comments

Hey! it seemed like it worked but it is selecting 0th and 1st column! It is weird but it is not selecting the third one
That's weird. Did you specify, [0,2] as in 1st column and 3rd column, or can we see the output you get ?
just edited question for the output screenshot with the input reaction_data. Yes I used ARRAY[ : , [0,2] ]
Text would have been better. Could you show the operations you did ? Because arr[:, [0,2]] shouldn't select the 1'st column. The only way i can think, it will select column 1 is when one do arr[:, 0:2] instead of arr[:, [0, 2]]
@NisargDave Could you report back ASAP, i will be going out in 10 minutes :)
|
0

You can do that with numpy.delete function easily as shown below:

a = np.array([[240.66666667, 171.22222222, 158.33333333],
              [218.66666667, 134.77777778, 143.33333333],
              [197.33333333, 118.55555556, 128.44444444],
              [195.22222222, 119.33333333, 126.11111111],
              [196.77777778, 118.55555556, 123.77777778],
              [183.11111111, 111.88888889, 118.88888889],
              [173.77777778, 106.77777778, 114.44444444]])

a = np.delete(a,1,axis=1)

With that piece of code, you can get the output you want.

Output: 
[[240.66666667 158.33333333]
 [218.66666667 143.33333333]
 [197.33333333 128.44444444]
 [195.22222222 126.11111111]
 [196.77777778 123.77777778]
 [183.11111111 118.88888889]
 [173.77777778 114.44444444]]

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.