I Have a 3D array composed of various columns. I just want to slice the last column. The array looks like the following:
JavaScript
x
7
1
array([[[5.45454545e-01, 6.36363636e-01, 7.27272727e-01, ,
2
1.00000000e+00, 0.00000000e+00, 9.09090909e-02],
3
[5.45454545e-01, 6.36363636e-01, 7.27272727e-01, ,
4
1.00000000e+00, 0.00000000e+00, 9.09090909e-02],
5
[5.45454545e-01, 6.36363636e-01, 7.27272727e-01, ,
6
1.00000000e+00, 0.00000000e+00, 9.09090909e-02]
7
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.
JavaScript
1
2
1
df[:, :-1]
2
Advertisement
Answer
IUUC, you can use:
JavaScript
1
2
1
a[ , :-1] # equivalent to a[:,:,:-1]
2
Example input:
JavaScript
1
14
14
1
a = np.arange(3**3).reshape(3,3,3)
2
3
array([[[ 0, 1, 2],
4
[ 3, 4, 5],
5
[ 6, 7, 8]],
6
7
[[ 9, 10, 11],
8
[12, 13, 14],
9
[15, 16, 17]],
10
11
[[18, 19, 20],
12
[21, 22, 23],
13
[24, 25, 26]]])
14
matching output:
JavaScript
1
14
14
1
a[ , :-1]
2
3
array([[[ 0, 1],
4
[ 3, 4],
5
[ 6, 7]],
6
7
[[ 9, 10],
8
[12, 13],
9
[15, 16]],
10
11
[[18, 19],
12
[21, 22],
13
[24, 25]]])
14