Articles

# delete a row from a numpy array | delete multiple rows in numpy array

In this numpy tutorial,  we will discuss about:

• create numpy array
• how to delete a row from a numpy array?
• delete multiple rows in numpy array using delete()
• delete multiple rows in numpy array using delete() through slice parameter.

Before we start to learn how to delete a row from a numpy array, lets first understand about numpy and create numpy array.

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

It is a module 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.

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: Lets create a scenario to work upon

``````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 row:

[
[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]]``````

Here first row has been removed.

### create numpy array

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)
``````

Output: create numpy array result

``````[[ 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]]``````

Now lets use the above create numpy array to understand how to delete a row from a numpy array?

### Scenario 1: how to delete a row from a numpy array

Remove single row using delete in numpy or delete() method in numpy.

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

indexing starts with 0.

Syntax:

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

where,

1. array_data is the input numpy array

2. index specifies the row position to be deleted

3. axis=0 specifies row.

Lets delete a row from a numpy array.

Examplehow to delete a row from a numpy array?

In this example, we will learn how to delete a row from a 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 row from numpy array
print(numpy.delete(array_data, 0, axis=0))

print()

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

print()

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

Outputdelete in numpy - row 1, 3 and 5

From the above delete a row from a numpy array code, we are deleting the first, third and fifth rows separately.

``````[[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]]

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

[[ 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]]``````

We have learned to delete a row from a numpy array. Now lets see how we can delete multiple rows in numpy array.

Scenario -2 : Remove multiple rows using delete() method

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

indexing starts with 0.

Syntax:

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

where,

1. array_data is the input numpy array

2. indices specifies the row positions to be deleted

3. axis=0 specifies row.

Exampledelete multiple rows in numpy array

In this delete multiple rows in numpy array example, we will delete rows 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 rows from numpy array
print(numpy.delete(array_data, [0,2,4], axis=0))

``````

Outputdelete multiple rows in numpy array

From the above code, we are deleting the first, third and fifth rows at a time.

``````[[134 516 413 212 415  64 564  62]
[ 54  56  54   5   6   8   9   0]]``````

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

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

indexing starts with 0.

Syntax:

``numpy.delete(array_data, slice(start_row,end_row), axis=0)``

where,

1. array_data is the input numpy array

2. slice() function - start_row specifies the starting row position and end_row specifies the ending row position

3. axis=0 specifies row.

Exampledelete multiple rows in numpy array

In this example, we will delete rows 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 row to third row
print(numpy.delete(array_data, slice(0,3), axis=0))

print()

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

Outputdelete multiple rows in numpy array result

From the above code, we are deleting from first row to thitd row and third row to fifth row separately

``````[[54 56 54  5  6  8  9  0]
[ 1  2  3  5 23 23 32 12]]

[[ 34  56  43  22  45   6  54   2]
[134 516 413 212 415  64 564  62]]``````

Thus we wrap up our session on delete a row from a numpy array and delete multiple rows in numpy array.

Numpy

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