How to delete last N row of NumPy array

In this post, we are going to understand how to delete the last N row of the NumPy array with examples by using the slice operator and np.delete() method examples. To run all these programs, make sure numpy is installed on the local machine.

1.How to delete last N rows of NumPy array using loop


In this python program, we are iterating over the NumPy array using for loop and on each iteration, we are deleting the row from the NumPy array.

  • When value ind is 1, the last row will delete from the NumPy array.
  • On the next iteration, the last two rows will delete from the NumPy array.
  • When the value Ind is 3 the loop will terminate by a break statement
#python 3 program to delete last n row of numpy array
import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])


for ind in range(1, len(origanlArr)+1):    
    if(ind==3):
     break;
    print("NumPy array after deletig rows:\n ", origanlArr[:-ind])
  


Output

NumPy array after deletig rows:
  [[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]
 [ 9 18 27 36 45 54 63]]
NumPy array after deletig rows:
  [[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]]

2. How to delete last N row of NumPy array using slicing


To Delete the last N row of the NumPy array using slicing. We will use Index brackets ([]) to delete rows from the NumPy array. We can select single or multiple rows using slicing. To select a single row by index we use this syntax.

ndarray[rowindex]

To select multiple rows by index we use this syntax

Syntax

ndarray[Startindex : EndIndex ]

#for negative slicing
ndarray[-startIndex:]

Parameters

  • StartIndex : EndIndex: It will select the row start from startindex till to EndIndex-1.

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])


print("NumPy array after deleting last one row :\n", origanlArr[:-1]) 

print("NumPy array after deleting last five  rows :\n", origanlArr[:-3])  

print("NumPy array after deleting last 5  rows :\n", origanlArr[:-5])

Output

NumPy array after deleting last one row :
 [[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]
 [ 9 18 27 36 45 54 63]]
NumPy array after deleting last five  rows :
 [[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]]
NumPy array after deleting last 5  rows :
 [[ 3  6  9 12 15 18 21]]

3. How to delete last 2 rows of NumPy array


In this example we will learn how to delete last N row of NumPy array s of NumPy array.and return new array.We can create a new numpy array by selecting or deleting the last N rows from numpy array.In this example We are deleteing last row from numpy array and creating an result array with 5 rows.

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

resultarr = origanlArr[:5]

print("NumPy array after deleting last 2 rows :\n", resultarr )

Output

NumPy array after deleting last one row :
 [[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]
 [ 9 18 27 36 45 54 63]]

4. Delete row by index using np.delete()


We can delete the last n rows from numpy array using np.delete() function by passing the indices.The np.delete() methods takes following agrumets.

  • The first parameter in numpy.delete() function is numpy array
  • The second parameters is row index.
  • The third parameter is axis =0 that means it will delete first row.
import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])


resArr = np.delete(origanlArr, [1, 3, 5], 0)

print("NumPy array after deleting last one row :\n", resArr)

Output

NumPy array after deleting last two rows :
 [[ 3  6  9 12 15 18 21]
 [11 22 33 44 55 66 77]
 [ 9 18 27 36 45 54 63]]

5. Delete mutiple rows by indexes using slicing


We can delete multiple rows and columns from the numpy array using numpy.delete() function using slicing.The first argument is numpy array the second argument is slicing and the third argument is the axis =0 means, which will delete multiple rows.

  • slice[:stop] : if we pass only one argument to slice()
  • slice[start:stop] : if we pass two argument to slice() function
  • slice[start:stop:step] : if we pass three arguments to slice() function
import numpy as np
   
origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])


 
newarr = np.delete(origanlArr , slice(4), 0)
 
print('new array1:\n',newarr)

Output

new array1:
 [[ 9 18 27 36 45 54 63]
 [10 20 30 40 50 60 70]]

Summary

In this post,we have learned how to delete last N row of NumPy array by using slicing and np.delete() method of numpy libaray with examples.