[SciPy-User] delete rows and columns

Puneeth Chaganti punchagan at gmail.com
Wed Mar 14 06:25:16 EDT 2012


On Wed, Mar 14, 2012 at 3:46 PM, Lee <lchaplin13 at gmail.com> wrote:
> Hi all,
>
> first time here, sorry if I am not posting in the right group.
> I am trying to run the below example from numpy docs:
>
> import numpy as np
> print np.version.version #1.6.1 (win7-64, py2.6)
>
> a = np.array([0, 10, 20, 30, 40])
> np.delete(a, [2,4]) # remove a[2] and a[4]
> print a
> a = np.arange(16).reshape(4,4)
> print a
> np.delete(a, np.s_[1:3], axis=0) # remove rows 1 and 2
> print a
> np.delete(a, np.s_[1:3], axis=1) # remove columns 1 and 2
> print a
>
> Basically I am trying to delete some column/rows from an array or a
> matrix.
> It seems that delete doesn't work I expect (and advertised). Am I
> missing something?

np.delete does not change the array in place. It does work as
advertised, which says

"""
Return a new array with sub-arrays along an axis deleted.
"""

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> np.delete(arr, 1, 0)
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])

HTH,
Puneeth



More information about the SciPy-User mailing list