In this article we will discuss how to remove elements , rows and columns from 1D & 2D numpy array using np.delete().

## np.delete()

Python’s Numpy library provides a method to delete elements from a numpy array based on index position i.e.

Args:

• arr : Numpy array
• obj : index position or list of index positions to be deleted from numpy array arr
• axis : Axis along which we want to delete. If 1 then dlete columns, if 0 then delete rows and if None then apply delete on flattened array.

Returns:

• Returns a copy of array arr by deleting elements at index positions pointed by obj. If axis is None then returns a flattened array.

Let’s see how to use np.delete() to remove elements by index positions from 1D & 2D numpy arrays and also how to delete rows & columns from 2D numpy arrays.

First of all import numpy module i.e.

## Delete an element in Numpy Array by Index position

Suppose we have a numpy array of numbers i.e.

Now let’s delete the element from above numpy array at index position 2 i.e.

Output:

In np.delete() we passed the numpy array and also the index position that we want to be deleted.

## Delete multiple element in Numpy Array by Index position

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.

Output:

It deletes the elements at index position 1,2 and 3.

## Delete rows & columns from a 2D Numpy Array

Suppose we have a 2D numpy array i.e.

Output:

Now let’s see how to delete rows and columns from it based on index positions.

### Delete a column in Numpy Array by column number

To delete a column from a 2D numpy array using np.delete() we need to pass the axis=1 along with numpy array and index of column i.e.

Output:

It will delete the column at index position 1 from the above created 2D numpy array.

### Delete multiple columns in Numpy Array by column number

Pass axis=1 and list of column numbers to be deleted along with numpy array to np.delete() i.e.

Output:

It will delete the column at index position 2 and 3 from the above created 2D numpy array.

### Delete a row in 2D Numpy Array by row number

Our original 2D numpy array arr2D is,

To delete a row from a 2D numpy array using np.delete() we need to pass the axis=0 along with numpy array and index of row i.e. row number,

Output:

It will delete the row at index position 0 from the above created 2D numpy array.

### Delete multiple rows in Numpy Array by row number

Our original 2D numpy array arr2D is,

Pass axis=0 and list of row numbers to be deleted along with numpy array to np.delete() i.e.

Output:

It will delete the row at index position 1 and 2 from the above created 2D numpy array.

### Delete specific elements in 2D Numpy Array by index position

Our original 2D numpy array arr2D is,

When we don’t pass axis argument to np.delete() then it’s default value is None, which means 2D numpy array will be flattened for deleting elements at given index position.  Let’s use np.delete() to delete element at row number 0 and column 2 from our 2D numpy array,

Output:

It returns the flattened copy of 2D numpy array after deleting element. We passed 2 because in flattened 2d matrix we gor the number from row and column position i.e. position in flattened array = row * no_of_columns + column. So, position in flattened array = 0 * no_of_columns + 2 = 2.

We have created a function to do this calculation and delete element from 2D numpy array by row and column position i.e.

let’s use this to delete element at row 1& column 1 from our 2D numpy array i.e.

Output:

It returns the flattened copy of 2D numpy array after deleting element at row 1 and column 1.

Complete example is as follows:

Output:

## Join 1500+ Python & C++ developers to get more Tips &  Tutorials like this.

Python Recommendations: C++ & C++11 Recommendations: If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.