Test Test
Test Test

Reputation: 505

How to delete column in 3d numpy array

I have a numpy array that looks like this

[
[[1,2,3], [4,5,6]],
[[3,8,9], [2,9,4]],
[[7,1,3], [1,3,6]]
]

I want it like this after deleting first column

[
[[2,3], [5,6]],
[[8,9], [9,4]],
[[1,3], [3,6]]
]

so currently the dimension is 3*3*3, after removing the first column it should be 3*3*2

Upvotes: 7

Views: 4167

Answers (2)

Julu Ahamed
Julu Ahamed

Reputation: 9133

Since you are using numpy I'll mention numpy way of doing this. First of all, the dimension you have specified for the question seems wrong. See below

x = np.array([
[[1,2,3], [4,5,6]],
[[3,8,9], [2,9,4]],
[[7,1,3], [1,3,6]]
])

The shape of x is

x.shape
(3, 2, 3)

You can use numpy.delete to remove a column as shown below

a = np.delete(x, 0, 2)
a
array([[[2, 3],
    [5, 6]],

   [[8, 9],
    [9, 4]],

   [[1, 3],
    [3, 6]]])

To find the shape of a

a.shape
(3, 2, 2)

Upvotes: 8

Alexander
Alexander

Reputation: 109546

You can slice it as so, where 1: signifies that you only want the second and all remaining columns from the inner most array (i.e. you 'delete' its first column).

>>> a[:, :, 1:]
array([[[2, 3],
        [5, 6]],

       [[8, 9],
        [9, 4]],

       [[1, 3],
        [3, 6]]])

Upvotes: 11

Related Questions