Skip to content
Advertisement

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]

Advertisement

Answer

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]]])
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement