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

miador
  • 358
  • 1
  • 5
  • 20
Nisarg Dave
  • 103
  • 1
  • 6

2 Answers2

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]])
>>> 
han solo
  • 6,390
  • 1
  • 15
  • 19
  • 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 – Nisarg Dave Aug 22 '20 at 19:12
  • That's weird. Did you specify, [0,2] as in 1st column and 3rd column, or can we see the output you get ? – han solo Aug 22 '20 at 19:16
  • just edited question for the output screenshot with the input reaction_data. Yes I used ARRAY[ : , [0,2] ] – Nisarg Dave Aug 22 '20 at 19:29
  • 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]]` – han solo Aug 22 '20 at 19:32
  • @NisargDave Could you report back ASAP, i will be going out in 10 minutes :) – han solo Aug 22 '20 at 19:43
  • Just worked, it was weird. I did exit the virtual environment and reinitiated process and now it is working. Thanks! – Nisarg Dave Aug 22 '20 at 19:46
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]]
miador
  • 358
  • 1
  • 5
  • 20