In this post, we are going to learn about, NumPy array delete multiple rows and columns logic. To delete columns and rows from the numpy array np. delete() function is used along with this we can pass the list of rows and columns indexes or using slicing.s
Numpy.delete() function
The numpy. delete() function returns a new array after deleting rows and columns along with the given axis.
Syntax
numpy.delete(array,obj,axis=0)
Parameters
- array: The array in which rows and columns to be deleted
- obj: index position, list of indexes position or slice index to be delete from numpy array
- axis: row or column along which to delete values from numpy arraydefulat value is 0.
- axis =0 : it deletes the rows
- axis=1 : it deletes the colums
1. 2D numpy array Delete Multiple rows and columns
In this Python code example, we are passing column indexes as a list to numpy.delete() function parameter and we have specified axis =1 means it will delete the column.
Numpy Delete Multiple columns by indexes
import numpy as np
myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])
myarr = np.delete(myarr, [1,3], axis=1)
print(myarr)
Output
[[12 70]
[20 60]
[ 3 9]
[ 4 12]]
Delete Multiple row by indexes from 2D array
In this Python code example, we are passing rows indexes as a list to numpy.delete() function as parameter.
- The first parameterin numpy.delete() function is numpy array
- The second parameters is the list of rows indexes
- The third parameter is axis =0 that means it will delete rows.
import numpy as np
myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])
myarr = np.delete(myarr, [1,3], axis=0)
print(myarr)
Output
[[12 14 70 80]
[ 3 6 9 12]]
2.Delete Multiple rows and columns using slicing
We can delete multiple rows and columns from numpy array using numpy.delete() function using slicing.The first argument is numpy array the second agrument is slicing and third argument is the axis =1 mean,i will delete multiple columns.
- 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
Delete Multiple columns using slicing
import numpy as np
myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])
newarr = np.delete(myarr, slice(2), 1)
print('new array1:',newarr)
delarr = np.delete(myarr, slice(1, 3), 1)
print('delarr array:',newarr)
Output
new array1 :
[[70 80]
[60 50]
[ 9 12]
[12 16]]
delarr array :
[[12 80]
[20 50]
[ 3 12]
[ 4 16]]
Delete multiple rows using slice
We can delete multiple rows and rows from the numpy array by using numpy.delete() function using slicing.Where s The first argument is numpy array and the second argument is slicing and third argument is the axis =1 mean,i will delete multiple columns.
import numpy as np
myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])
newarr = np.delete(myarr, slice(1, 3), 0)
print('after deleting mutiple rows :',newarr)
after deleting mutiple rows :
[[12 14 70 80]
[ 4 8 12 16]]
3. Delete multiple rows and columns using np.s_[]
In this example are deleting multiple rows andcolumns using np.s_[] we can specify np.s_[start,stop,step]
Delete Mutiple columns using np.s_[]
import numpy as np
myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])
newarr = np.delete(myarr, np.s_[:2], 0)
print('delete mutiple row with slicing stop :',newarr)
#delete mutiple row by using start and stop
newarr1 = np.delete(myarr, np.s_[1:3], 0)
print('delete mutiple row with slicing start,stop:',newarr1 )
##delete mutiple row by using start and stop,step
newarr2 = np.delete(myarr, np.s_[1:4:2], 0)
print("delete mutiple row by using start and stop :",newarr2)
Output
delete mutiple row with slicing stop
[[ 3 6 9 12]
[ 4 8 12 16]]
delete mutiple row with slicing start,stop
[[12 14 70 80]
[ 4 8 12 16]]
delete mutiple row by using start and stop :
[[12 14 70 80]
[ 3 6 9 12]]
Delete Mutiple rows using np.s_[]
import numpy as np
myarr = np.array([[12,14,70,80],[20,75,60,50],[3,6,9,12],[4,8,12,16]])
newarr = np.delete(myarr, np.s_[:2], 1)
print('delete mutiple columns with slicing stop :',newarr)
#delete mutiple columns by with start and stop
newarr1 = np.delete(myarr, np.s_[1:3], 1)
print('delete mutiple columns with slicing start,stop:',newarr1 )
##delete mutiple columns with start and stop,step
newarr2 = np.delete(myarr, np.s_[1:4:2], 1)
print("delete mutiple columns by using start and stop :",newarr2)
Output
delete mutiple columns with slicing stop : [[70 80]
[60 50]
[ 9 12]
[12 16]]
delete mutiple columns with slicing start,stop: [[12 80]
[20 50]
[ 3 12]
[ 4 16]]
delete mutiple columns by using start and stop : [[12 70]
[20 60]
[ 3 9]
[ 4 12]]