Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete columns in numpy.array

I would like to delete selected columns in a numpy.array . This is what I do:

n [397]: a = array([[ NaN,   2.,   3., NaN],    .....:        [  1.,   2.,   3., 9]])  In [398]: print a [[ NaN   2.   3.  NaN]  [  1.   2.   3.   9.]]  In [399]: z = any(isnan(a), axis=0)  In [400]: print z [ True False False  True]  In [401]: delete(a, z, axis = 1) Out[401]:  array([[  3.,  NaN],        [  3.,   9.]]) 

In this example my goal is to delete all the columns that contain NaN's. I expect the last command to result in:

array([[2., 3.],        [2., 3.]]) 

How can I do that?

like image 265
Boris Gorelik Avatar asked Oct 29 '09 10:10

Boris Gorelik


People also ask

How do I remove a column from a NumPy array?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.

How do I delete multiple columns in NumPy?

To delete multiple elements from a numpy array by index positions, pass the numpy array and list of index positions to be deleted to np. delete() i.e. It deleted the elements at index position 1,2 and 3 from the numpy array. It returned a copy of the passed array by deleting multiple element at given indices.

How do I delete multiple rows in NumPy?

delete() – The numpy. delete() is a function in Python which returns a new array with the deletion of sub-arrays along with the mentioned axis. By keeping the value of the axis as zero, there are two possible ways to delete multiple rows using numphy. delete().

How can I remove columns in NumPy array that contains non numeric values?

Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np. isnan() function.


2 Answers

Given its name, I think the standard way should be delete:

import numpy as np  A = np.delete(A, 1, 0)  # delete second row of A B = np.delete(B, 2, 0)  # delete third row of B C = np.delete(C, 1, 1)  # delete second column of C 

According to numpy's documentation page, the parameters for numpy.delete are as follow:

numpy.delete(arr, obj, axis=None)

  • arr refers to the input array,
  • obj refers to which sub-arrays (e.g. column/row no. or slice of the array) and
  • axis refers to either column wise (axis = 1) or row-wise (axis = 0) delete operation.
like image 163
Steve Tjoa Avatar answered Sep 22 '22 11:09

Steve Tjoa


Example from the numpy documentation:

>>> a = numpy.array([[ 0,  1,  2,  3],                [ 4,  5,  6,  7],                [ 8,  9, 10, 11],                [12, 13, 14, 15]])  >>> numpy.delete(a, numpy.s_[1:3], axis=0)                       # remove rows 1 and 2  array([[ 0,  1,  2,  3],        [12, 13, 14, 15]])  >>> numpy.delete(a, numpy.s_[1:3], axis=1)                       # remove columns 1 and 2  array([[ 0,  3],        [ 4,  7],        [ 8, 11],        [12, 15]]) 
like image 42
Nikolay Frick Avatar answered Sep 24 '22 11:09

Nikolay Frick