Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the all columns except last column in 3D numpy array?

I Have a 3D array composed of various columns. I just want to slice the last column. The array looks like the following:

array([[[5.45454545e-01, 6.36363636e-01, 7.27272727e-01, ...,
         1.00000000e+00, 0.00000000e+00, 9.09090909e-02],
        [5.45454545e-01, 6.36363636e-01, 7.27272727e-01, ...,
         1.00000000e+00, 0.00000000e+00, 9.09090909e-02],
        [5.45454545e-01, 6.36363636e-01, 7.27272727e-01, ...,
         1.00000000e+00, 0.00000000e+00, 9.09090909e-02]

I have tried the following code. But it only shows the last column while I want to show all columns values except the last column.

df[:, :-1]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

IUUC, you can use:

a[..., :-1]  # equivalent to a[:,:,:-1]

Example input:

a = np.arange(3**3).reshape(3,3,3)

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]]])

matching output:

a[..., :-1]

array([[[ 0,  1],
        [ 3,  4],
        [ 6,  7]],

       [[ 9, 10],
        [12, 13],
        [15, 16]],

       [[18, 19],
        [21, 22],
        [24, 25]]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading