Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy python array slicing

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

like image 767
Nisarg Dave Avatar asked Jun 14 '26 19:06

Nisarg Dave


1 Answers

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]])
>>> 
like image 133
han solo Avatar answered Jun 17 '26 08:06

han solo