Articles

# How to Delete Columns from a numpy Array | delete multiple columns in numpy array

In this numpy tutorial,  we will discuss about.

• create numpy array of given size,
• how to delete a column from a numpy array?
• delete multiple columns in numpy array
• numpy delete array slice parameter

Before we learn to delete numpy column, lets create one numpy array.

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python.

Syntax to import:

``import numpy``

We can also use alias for the module

For example,

``import numpy as np``

We can directly use np to call the numpy module.

### Create Numpy Array

An array is an one dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

``numpy.array(elements)``

where, elements are the input data elements.

Scenario:

``````Input array:

[[ 34  56  43  22  45   6  54   2]
[134 516 413 212 415  64 564  62]
[  2   3   4   5   6   7   8   5]
[ 54  56  54   5   6   8   9   0]
[  1   2   3   5  23  23  32  12]]

After removing particular column:

[[ 34  56  43  22  45   6  54   ]
[134 516 413 212 415  64 564  ]
[  2   3   4   5   6   7   8   ]
[ 54  56  54   5   6   8   9   ]
[  1   2   3   5  23  23  32  ]]``````

Let's create an 2 dimensional numpy array with 5 rows.

``````#importing the numpy module
import numpy

#create an array with 8 elements - integer type
#in each row
array_data=numpy.array([[34,56,43,22,45,6,54,2],
[134,516,413,212,415,64,564,62],
[2,3,4,5,6,7,8,5],
[54,56,54,5,6,8,9,0],
[1,2,3,5,23,23,32,12]])

#display numpy array
print(array_data)
``````

Outputcreate numpy array of given size

``````[[ 34  56  43  22  45   6  54   2]
[134 516 413 212 415  64 564  62]
[  2   3   4   5   6   7   8   5]
[ 54  56  54   5   6   8   9   0]
[  1   2   3   5  23  23  32  12]]``````

Lets use above array to delete numpy column.

Scenario -1 : Remove single column using delete() method

Here we are going to use delete() method , which is available in numpy module to delete a particular column by specifying index position.

indexing starts with 0.

Syntax:

``numpy.delete(array_data, index, axis=1)``

where,

1. array_data is the input numpy array

2. index specifies the column position to be deleted

3. axis=1 specifies column.

Examplehow to delete a column from a numpy array?

In this delete numpy column example, we will delete columns in the numpy array.

``````#importing the numpy module
import numpy

#create an array with 8 elements - integer type
#in each row
array_data=numpy.array([[34,56,43,22,45,6,54,2],
[134,516,413,212,415,64,564,62],
[2,3,4,5,6,7,8,5],
[54,56,54,5,6,8,9,0],
[1,2,3,5,23,23,32,12]])

#remove first column from numpy array
print(numpy.delete(array_data, 0, axis=1))

print()

#remove third column from numpy array
print(numpy.delete(array_data, 2, axis=1))

print()

#remove fifth column from numpy array
print(numpy.delete(array_data, 4, axis=1))``````

Outputdelete numpy column result

From the above delete numpy column code, we are deleting the first, third and fifth columns separately.

``````[[ 56  43  22  45   6  54   2]
[516 413 212 415  64 564  62]
[  3   4   5   6   7   8   5]
[ 56  54   5   6   8   9   0]
[  2   3   5  23  23  32  12]]

[[ 34  56  22  45   6  54   2]
[134 516 212 415  64 564  62]
[  2   3   5   6   7   8   5]
[ 54  56   5   6   8   9   0]
[  1   2   5  23  23  32  12]]

[[ 34  56  43  22   6  54   2]
[134 516 413 212  64 564  62]
[  2   3   4   5   7   8   5]
[ 54  56  54   5   8   9   0]
[  1   2   3   5  23  32  12]]``````

Similarly you can also delete last column numpy array or any particular column.

Scenario -2 : delete multiple columns in numpy array

Here we are going to use delete() method , which is available in numpy module to delete multiple columns by specifying index positions in a list.

indexing starts with 0.

Syntax:

``numpy.delete(array_data, [indices], axis=1)``

where,

1. array_data is the input numpy array

2. indices specifies the column positions to be deleted

3. axis=1 specifies column.

Exampledelete multiple columns in numpy array using delete method

In this delete multiple columns in numpy array example, we will delete columns in the numpy array.

``````#importing the numpy module
import numpy

#create an array with 8 elements - integer type
#in each row
array_data=numpy.array([[34,56,43,22,45,6,54,2],
[134,516,413,212,415,64,564,62],
[2,3,4,5,6,7,8,5],
[54,56,54,5,6,8,9,0],
[1,2,3,5,23,23,32,12]])

#remove first,third and fifth columns from numpy array
print(numpy.delete(array_data, [0,2,4], axis=1))``````

Outputdelete multiple columns in numpy array result

From the above delete multiple columns in numpy array code, we are deleting the first, third and fifth columns at a time.

``````[[ 56  22   6  54   2]
[516 212  64 564  62]
[  3   5   7   8   5]
[ 56   5   8   9   0]
[  2   5  23  32  12]]``````

Now lets see delete multiple columns in numpy array using delete method through slice parameter.

Scenario - 3 : Remove multiple columns using delete() method through slice parameter

Here we are going to use delete() method , which is available in numpy module to delete multiple columns by specifying slice() parameter. This will delete multiple columns in numpy array based on the given range. It will delete columns from start row to end row specified.

indexing starts with 0.

Syntax:

``numpy.delete(array_data, slice(start_column,end_column), axis=0)``

where,

1. array_data is the input numpy array

2. slice() function - start_column specifies the starting column position and end_column  specifies the ending column position

3. axis=1 specifies column.

Exampledelete multiple columns in numpy array in given range

In this delete multiple columns in numpy array example, we will delete columns in the numpy array.

``````#importing the numpy module
import numpy

#create an array with 8 elements - integer type
#in each row
array_data=numpy.array([[34,56,43,22,45,6,54,2],
[134,516,413,212,415,64,564,62],
[2,3,4,5,6,7,8,5],
[54,56,54,5,6,8,9,0],
[1,2,3,5,23,23,32,12]])

#remove from first column to third column
print(numpy.delete(array_data, slice(0,3), axis=1))

print()

#remove from  third column to fifth column
print(numpy.delete(array_data, slice(2,5), axis=1))``````

Outputnumpy delete array slice result

From the above numpy delete array slice code, we are deleting from first column to third column and third column to fifth column separately

``````[[ 22  45   6  54   2]
[212 415  64 564  62]
[  5   6   7   8   5]
[  5   6   8   9   0]
[  5  23  23  32  12]]

[[ 34  56   6  54   2]
[134 516  64 564  62]
[  2   3   7   8   5]
[ 54  56   8   9   0]
[  1   2  23  32  12]]``````

Thus we can in numpy delete array slice any given range as shown above.

This wraps up our session on how to delete a column from a numpy arraynumpy delete array slice, delete multiple columns in numpy array.

Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com 