简体   繁体   中英

How to delete a column in Numpy array

my array look like this (9 columns)

[('2012-07', 'abc', 'EXECUTIVE', '149', '16 TO 18', 150., 1995,'apt', 1000000)
 ('2012-10', 'abc', 'EXECUTIVE', '194', '22 TO 24', 163., 1987, 'ma',  1010000)
  ('2014-12', 'abc', 'EXECUTIVE', '190', '19 TO 21', 150., 1987, 'ma', 1000000)] 

print(a.shape)
output >> (3,)
print(len(a))
output >> 3

I want to delete the last 2nd column, so it will look like this

[('2012-07', 'abc', 'EXECUTIVE', '149', '16 TO 18', 150., 1995, 1000000)
 ('2012-10', 'abc', 'EXECUTIVE', '194', '22 TO 24', 163., 1987, 1010000)
  ('2014-12', 'abc', 'EXECUTIVE', '190', '19 TO 21', 150., 1987, 1000000)] 

I tried the np.delete function:

b = np.delete(a,7,axis=1)

Error >> AxisError: axis 1 is out of bounds for array of dimension 1

My array has multiple rows with 9 columns, just want to delete one column.

Any help would be very much appreciated. Thanks!

a.shape: (3,), it means there's only 3 element on axis 0, no axis 1. Should check how your matrix data assigned to a.

import numpy as np

a = [('2012-07', 'abc', 'EXECUTIVE', '149', '16 TO 18', 150., 1995, 'apt', 1000000),
     ('2012-10', 'abc', 'EXECUTIVE', '194', '22 TO 24', 163., 1987, 'ma',  1010000),
     ('2014-12', 'abc', 'EXECUTIVE', '190', '19 TO 21', 150., 1987, 'ma',  1000000)]

b = np.array(a)

c = np.delete(b,7,axis=1)

While the other answer is correct for a regular array it doesn't answer the question in the OP which is for a structured array . In this case you may create a new array with just the columns you need, ie instead of deleting the unwanted column you select the wanted columns.

Example to delete the seventh column:

b = a[list(a.dtype.names[:7] + arr.dtype.names[8:])]

You can also use the helper function drop_fields :

from numpy.lib import recfunctions as rfn
b = rfn.drop_fields(a, a.dtype.names[7])

The first thing to note is that you have a structured Numpy array.

It must be created with dtype parameter, otherwise all field types are U... (Unicode strings of certain size). I created such an array running:

a = np.array([
    ('2012-07', 'abc', 'EXECUTIVE', '149', '16 TO 18', 150., 1995, 'apt', 1000000),
    ('2012-10', 'abc', 'EXECUTIVE', '194', '22 TO 24', 163., 1987, 'ma',  1010000),
    ('2014-12', 'abc', 'EXECUTIVE', '190', '19 TO 21', 150., 1987, 'ma',  1000000)],
    dtype='U7, U3, U10, i4, U10, f4, i4, U4, i4')

Note that even if you didn't pass field names (only types like above), Numpy still assigns default filed names, as f0 , f1 and so on.

To print field names you can run eg a.dtype.names . In the above case I got:

('f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8')

so probably your task is to delete f7 field (check in your environment). Maybe you have some field names explicitely assigned by your code, so use the last but one name instead.

To get a copy of a Numpy array with this column dropped, you can run:

import numpy.lib.recfunctions as rcf
b = rcf.drop_fields(a, 'f7')

or save it back under a if you wish.

A remark concerning the solution proposed by Jason Yang :

If you run b.shape you will get (3, 9) , whereas your sample contains (3,) and the array which I created has just this shape.

So your array must have been created some other way than in his solution.

Your output from:

print(a.shape)
output >> (3,)

shows that the numpy array is 1-dimensional. It has 3 rows and 0 columns.

According to what you want, shape should return (3,9) denoting 3 rows and 9 columns.

Here is how you can do that:

import numpy as np

original_array = np.array([('2012-07', 'abc', 'EXECUTIVE', '149', '16 TO 18', 150., 1995, 'apt', 1000000),
            ('2012-10', 'abc', 'EXECUTIVE', '194', '22 TO 24', 163., 1987, 'ma',  1010000),
            ('2014-12', 'abc', 'EXECUTIVE', '190', '19 TO 21', 150., 1987, 'ma',  1000000)])

To delete the second last element, you can use negative index :

new_array = np.delete(my_array, -2, axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM