-->

delete last item in all rows and columns numpy.nda

2019-09-14 16:20发布

问题:

I am trying to delete the last item in both the rows and columns in my numpy.ndarray (type = class numpy.ndarray). My array has 30 rows and 180 columns (i.e. 180 values per row). I have tried numpy.delete but this simply removes the whole row/column.

To illustrate what I want to achieve I created the following example in Python using and array and nested for loops:

a = np.array([[[1,2,3,4,5,6],[1,2,3,4],[1,2,3,4]],[[1,2,3,4,5,6],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]]])
for list in a:
    for sublist in list:
        del sublist[-1]

Using

print(a) 

Gives the following array:

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

Using

print(list)

after the for loops gives:

[[1, 2, 3, 4, 5], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3, 4, 5], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]

Unfortunately using this on my array gives the following error:

TypeError: 'numpy.float64' object does not support item deletion

Thanks


Update: I am extracting my information from a grid NetCDF file. I have changed the word list to l since list is a Python keyword. This didn't change it for me.

This provides a good example of my array:

c = np.arange(5400).reshape(30,180)
for l in c:
    for i in l:
        del i[-1]

When I run this code I get the following error:

Traceback (most recent call last):   File "main.py", line 18, in <module>
    del i[-1] 
 TypeError: 'numpy.int64' object does not support item deletion

回答1:

del i[-1] is a list operation. np.array does not support that.

Count the occurrences of a specific value and remove them at the same time demonstrates the differences between lists and arrays when it comes to deletion.

Your example a is object dtype, containing lists

In [111]: a.shape
Out[111]: (11,)
In [112]: [len(i) for i in a]
Out[112]: [3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2]
In [113]: a[0]
Out[113]: [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4], [1, 2, 3, 4]]

a[0] is a 3 element list, with sublists of different length.

It's not clear what you want to delete. Delete elements from a, or elements from each element of a, or elements from the sublists of those elements.

Furthermore, if the real data is from NetCDF it might actually a multidimensional array. Or if object dtype, the elements might themselves be (2d) arrays.

In case, slicing is the right way to remove rows/columns from an array:

In [114]: a = np.arange(12).reshape(3,4)
In [115]: a
Out[115]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [116]: a[:-1, :-1]
Out[116]: 
array([[0, 1, 2],
       [4, 5, 6]])

The result is a view; it does not change a itself. a = a[:-1, :-1].copy() is the cleanest way to creates a reduced size array without leaving the any of the original around.