Python NumPy Delete – Complete Tutorial

In this Python tutorial, we will learn how to delete Numpy array in Python. Also, we will cover these topics.

  • Python numpy delete repeat
  • Python NumPy delete element
  • Python NumPy delete row
  • Python NumPy delete multiple rows
  • Python NumPy delete array
  • Python NumPy delete dimension
  • Python NumPy delete element by index
  • Python NumPy delete last column
  • Python numpy remove element by value
  • Python NumPy remove element from list
  • Python NumPy delete last element
  • Python NumPy delete zero rows
  • Python NumPy remove nan rows
  • Python numpy replace nan with 0

Python NumPy delete

  • In this section, we will discuss the working of the delete function in NumPy Python.
  • In Python, the np.delete() function is used to remove elements from the NumPy array. This function is available in the NumPy module and always returns an array with a specified subarray along with the mentioned axis.

Syntax:

Here is the Syntax of np.delete() function

numpy.delete
            (
             arr,
             obj,
             axis=None
            )
  • It consists of a few parameters
    • arr: This parameter specifies the input array which elements we want to be deleted.
    • obj: it can be subarray or number
    • axis: By default its value is None and it indicates the axis which is deleted from the array.

Example:

Let’s take an example and check how to remove elements by using the np.delete() function

import numpy as np

new_array = np.array([36,89,245,865,934,178,278,643])
result = np.delete(new_array,3)
print("Deleting item at index position at 3:", result)

In the above code first, we have imported a NumPy library and then use the numpy.delete() function in which we assigned the array along with the index position which we want to delete from the given array.

Here is the Screenshot of the following given code

Python NumPy delete
Python NumPy delete

Read Python NumPy Stack

Delete elements from Array in Python NumPy

Let us see, how to delete elements from an array in Python NumPy.

Source Code:

import numpy as np

new_arr = np.array([56, 33, 49, 93, 21, 19, 83, 77, 65, 46])
new_obj = [1, 4, 6]
new_output = np.delete(new_arr, new_obj)
print(new_output)

In the above program, we used the np.delete() function and assign ‘arr’ and ‘new_obj’ as an argument where ‘new_obj’ is a list of integer numbers. These number indicates the indexes of the items which we want to delete from an array.

Here is the Output of the following given code

Python NumPy delete
Python NumPy delete

This is how to delete elements from an array in Python NumPy.

Read Python NumPy round

Python Numpy delete repeat

  • To delete the repeat elements from the numpy array we can easily use the np.unique() Python function.
  • In Python the numpy.unique() function is used to find the unique items of an array and returns these unique items as a sorted numpy array.

Syntax:

Here is the Syntax of np.unique() function in Python

numpy.unique
            (
             ar,
             return_index=False,
             return_inverse=False,
             return_counts=False,
             axis=None
            )

Example:

import numpy as np

values = np.array([[345,167,267,189,897],
                 [345,167,867,987,897],
                 [345,167,945,234,897]])

z = np.unique(values)
print(z)

In the above code, we used the np.unique() function and assigned array ‘values’ in it. Once you will print ‘z’ then the output will display the unique values.

Here is the execution of the following given code

Python numpy delete repeat
Python NumPy delete repeat

This is an example of a Python NumPy delete repeat.

Also read, Python Numpy unique

Python NumPy delete element

  • In this section, we will discuss how to delete an element from the NumPy array in Python.
  • In this example, we are going to use the np.delete() function and pass ‘new_arr’, object, and axis as an argument.

Example:

import numpy as np

new_arr = np.array([[67 ,156, 986, 67],
                  [56, 156, 345, 897],
                  [45, 219, 456, 976]])
result = np.delete(new_arr, 2, axis=1)
print(result)

Here is the implementation of the following given code

Python NumPy delete element
Python NumPy delete element

This is how to delete an element from a Python NumPy array.

Read Python NumPy repeat

Python NumPy delete row

  • In this section, we will discuss how to delete the row in a Numpy array in Python.
  • By using the np.delete() function we can perform this particular task. In Python, this function is used to delete any row or column from the numpy array. we have to just specify the axis and index position as an argument.

Source Code:

import numpy as np

new_arr = np.array([[14, 56, 67], [34, 67, 89], [65, 116, 32]])
z=[1,2]
result = np.delete(new_arr,z, axis=0)
print(result)

In the above code, we imported the numpy library and then initialize an array by using the np.array() method. Now use the numpy.delete() function and pass all the parameters within the function. Once you will print ‘result’ then the output will display the one-dimensional array.

You can refer to the below Screenshot

Python  NumPy delete row
Python NumPy delete row

This is how to delete a row from Python NumPy array.

Read Python NumPy Data types

Python NumPy delete multiple rows

  • In this section, we will discuss how to delete multiple rows in NumPy Python.
  • To perform this particular task we are going to use the arange() function for creating an array and then apply the slicing method [:]. In this example, we have mentioned that we want to remove the last 3 rows from a given array.

Source Code:

import numpy as np

new_arr = np.arange(48).reshape(8, 6)
print("Initailized array:",new_arr)
new_output = new_arr[:3]
print("Deleted multiple rows:",new_output)

You can refer to the below Screenshot

Python NumPy delete multiple rows
Python NumPy delete multiple rows

As you can see in the Screenshot the output displays the updated NumPy array. This is how to delete multiple rows in Python NumPy.

Read Python NumPy 2d array

Deleting multiple rows in NumPy Python

By using the np.delete() function we can solve this problem. In this example, we are going to specify the object along with the axis whereas the object represents the list of index numbers that we want to remove from the array.

Syntax:

Here is the Syntax of np.delete() function

numpy.delete
            (
             arr,
             obj,
             axis=None
            )

Source Code:

import numpy as np

ini_arr = np.arange(36).reshape(6, 6)
print("Initailized array:",ini_arr)
z= np.delete(ini_arr, [2, 4, 1], axis=0)
print("Deleted multiple rows:",z)

Here is the Output of the following given code

Python NumPy delete multiple rows
Python NumPy delete multiple rows

As you can see in the Screenshot the output displays the new array that contains only the first 3-rows.

Read Python NumPy 3d array

Python NumPy delete array

  • In this Program, we will discuss how to delete an array in NumPy Python.
  • To do this task we are going to apply the np.setdiff1d() function. In Python, this function is used to set the difference of two numpy arrays or we can say that we have to find the values from the first array which are not available in the second array as per the condition of the arguments.

Syntax:

Here is the Syntax of np.setdiff1d() method

numpy.setdiff1d
               (
                ar1,
                ar2,
                assume_unique=False
               )
  • It consists of a few parameters
    • ar1: This parameter indicates the input array.
    • ar2: This specifies the numpy array which we want to compare with another array.
    • assume_unique: By default it takes ‘False’ value and if it is true then it indicates the arrays are both assumed to be unique.

Example:

Let’s take an example and understand the working of np.setdiff1d() function in NumPy Python

import numpy as np

new_arr = np.arange(49).reshape(7, 7)
del_ele = [33, 42, 16]
final_output = np.setdiff1d(new_arr, del_ele)
print(final_output)

In the above code, we imported a numpy library and then initialize an array by using the np.arange() function by taking the order (7,7).

Now declare a variable ‘final_output’ and assigned a function np.setdiff1d(). Once you will print ‘final_output’ then the result will display the new array.

Here is the Screenshot of the following given code

Python NumPy delete array
Python NumPy delete array

As you can see in the Screenshot the output displays the list [33, 42, 16] has been deleted from the NumPy array.

Read Python NumPy Split

Python NumPy delete dimension

  • In this section, we will discuss how to delete the dimension axis in the NumPy array by using Python.
  • In this example, we are going to use the np.delete() function along with the axis. In this method if axis=0 then it indicates the row of the first dimension and if axis=1 then it represents the column in a second dimension.
  • Now we want to delete the axis from numpy.delete() function. First, we will delete the third row from the given array by using (new_arr, 2, 0). Once you will print ‘rem_ele’ then the output will display the new array.

Example:

import numpy as np
#creation of array
new_arr = np.array([[2,3,4,5],
              [45,23,11,76],
              [67,84,108,96],
              [12,34,67,89]])
print(new_arr)
# delete third row
rem_ele = np.delete(new_arr, 2, 0)
print(rem_ele)
# delete third column
rem_ele2 = np.delete(new_arr,2, 1)
print(rem_ele2)

You can refer to the below Screenshot

Python NumPy delete dimension
Python NumPy delete a dimension

This is an example of Python NumPy delete a dimension.

Read Python NumPy Normalize

Delete dimension from NumPy array – Another method

To perform this particular task we are going to use the np.ones() function for creating an array and then use the slicing method for removing the axis in the np.ones() function.

Syntax:

Here is the Syntax of np.ones() function

numpy.ones
          (
           shape,
           dtype=None,
           order='C',
           *,
           like=None
          )

Let’s take an example and check how to use the np.ones() function for creating an array along with the axis

Example:

import numpy as np

arr = np.ones((6, 6, 2))
print("Creation of array:",arr)
z = arr[:, :, 0]
print("Deleting dimension from array:",z.shape)

Here is the implementation of the following given code

Python NumPy delete dimension
Python NumPy delete a dimension

Check out, Python NumPy Random

Python NumPy delete element by index

  • Let us see how to delete elements by index position in NumPy Python.
  • In this program first, we will import a numpy library and then initialize an array by using arange() function. After that declare a variable ‘z’ that indicates the object and assign a list of numbers in it. Now use the np.delete() function and pass an array ‘new_val’ as a parameter.

Example:

import numpy as np

new_val = np.arange(12)
z=[0,4,2]
result = np.delete(new_val, z)
print("Deleted elements at specific index:",result)

Here is the execution of the following given code

Python NumPy delete element by index
Python NumPy delete element by index

As you can see in the Screenshot the output displays the new array. This is how to delete elements by index from a Python NumPy array.

Read Python NumPy zeros

Python NumPy delete last column

  • In this section, we will discuss how to delete the last column in the NumPy array by using Python.
  • To perform this particular task we will create a simple array by using the np.arange() function and within this function pass integer value ’12’ in it along with shape and size. Now we want to delete the last column from the given array.
  • By using the numpy.delete() function we can easily delete the last column by assigning axis and object as an argument.

Source Code:

import numpy as np

new_val = np.arange(36).reshape(6,6)
print("Creation of array:",new_val)
result = np.delete(new_val,5,1)
print("Deleted last column from array:",result)

In the above code we used the function numpy.delete() and set the index number ‘5’ that indicates the last column will delete from a given array.

You can refer to the below Screenshot

Python NumPy delete last column
Python NumPy delete the last column

As you can see in the Screenshot the output displays that the last column has been deleted from the Numpy array by using numpy.delete() function.

This is how to delete the last column in Python NumPy.

Read Check if NumPy Array is Empty in Python

Python numpy remove element by value

  • In this Program, we will discuss how to remove elements based on value conditions by using NumPy Python.
  • In this example, we will create an array by using the np.array() function and within this method pass an integer value in it. Now apply the condition != and assign the integer number ‘369’. Once you will print ‘result’ then the output will display ‘369’ number has been deleted from an array.

Example:

import numpy as np

new_array = np.array([23,45,98,369,23,86,987,145,369,954,467,369])
result = new_array[new_array != 369]
print("Deleted item based on value:",result)

Here is the implementation of the following given code

Python numpy remove element by value
Python NumPy remove element by value

This is how to remove element by value from a Python NumPy array.

Read Python NumPy read CSV

Python NumPy remove element from list

  • Let us see how to remove an element from a list by using Python. By using the remove() function we can easily delete an item from the list. In this program, we want to remove element 98 from the list.
  • To delete or remove elements from the list we can also apply the del keyword. In Python, this is used to remove array elements. In this example, we want to remove the element at index ‘3’.

Example:

new_lis = [23, 67, 48, 98, 145, 897, 956, 345, 109, 654]

z= new_lis.remove(98)
del new_lis[3]
print(z)
print(" After removing element from list:",new_lis)

Here is the Screenshot of the following given code

Python NumPy remove element from list
Python NumPy remove an element from a list

As you can see in the Screenshot ’98’ has been removed from the list. This is how to remove elements from a list in Python.

Read Python NumPy log

Python NumPy delete last element

  • In this section, we will discuss how to delete the last element from NumPy array by using Python.
  • To do this task first we will create a simple array ‘new_array’ and then use the np.delete() function and within this function pass array along with ‘-1’ as an argument. This is a simple logic to delete the last element from an array.

Source Code:

import numpy as np

new_array = np.array([6,34,45,67,89])
result= np.delete(new_array, -1)
print(result)

Here is the implementation of the following given code

Python NumPy delete last element
Python NumPy delete the last element

As you can see in the Screenshot the last element has been deleted from an array

Read Python NumPy where with examples

Delete last element from NumPy array

By using the numpy.resize() method we can solve this problem. In Python the numpy.resize() method is used to define how many columns and rows you want to convert into a dimension.

Suppose we have a one-dimensional array that contains 6 elements and now I want to change the dimension for this we will pass (values,4) as an argument of the resize() method whereas values represent the array name.

Syntax:

Here is the Syntax of numpy.resize() method

numpy.resize
            (
             a,
             new_shape
            )

Source Code:

import numpy as np

values = np.array([54,118,78,145,768])
new_output= np.resize(values,4)
print("Deleted last element:",new_output)

Here is the execution of the following given code

Python NumPy delete last element
Python NumPy delete the last element

As you can see in the Screenshot the output displays that the last element has been removed from the array. This is how to delete the last element from a NumPy array.

Read Python NumPy linspace

Python NumPy delete zero rows

  • Here we can see how to delete zero rows in the NumPy array by using Python.
  • To delete a specific zero row in the array, we have to initialize a numpy array by using the np.array() method and within this function, we are going to assign zero arrays along with the numeric array.
  • Now use the numpy.delete() function and set axis=0 inside the function. Once you will print ‘result’ the zero rows will be removed from the array.

Example:

import numpy as np
# creation of array
new_val = np.array ([[0,0,0,0],[67,78,19,46],[0,0,0,0],[13,54,23,20]])
#object creation
z=[0,2]
result = np.delete(new_val, z, axis=0)
print("Deleted zero rows:",result)

Here is the implementation of the following given code

Python NumPy delete zero rows
Python NumPy delete zero rows

This is an example of Python NumPy delete zero rows.

Also read, Python NumPy concatenate

Python NumPy remove nan rows

  • In this section, we will discuss how to remove nan rows from the NumPy array by using Python.
  • In this example, we are going to use the np.array() function for creating an array and inside the function, we have assigned the np.nan values along with integer values. Now declare a variable ‘m’ and create a list of numbers that indicates the index position which we want to remove from the array.

Source Code:

import numpy as np
# Initialize array
new_array = np.array ([[np.nan,np.nan,np.nan,np.nan],[np.nan,np.nan,np.nan,np.nan],[12,78,54,156],[np.nan,np.nan,np.nan,np.nan]])
#object creation
m=[0,1,3]
new_output = np.delete(new_array, m, axis=0)
print("Deleted nan rows:",new_output)

Here is the Screenshot of the following given code

Python NumPy remove nan rows
Python NumPy remove nan rows

As you can see in the Screenshot the np.nan values have been removed from an array by using numpy.delete() function

Read Python sort NumPy array

Python numpy replace nan with 0

  • In this section, we will discuss how to replace nan value with zero by using NumPy Python.
  • By using the np.nan_to_num() method we can perform this particular task. In Python, the np.nan_to_num() method is used for replacing nan values with zeros.

Syntax:

Here is the Syntax of numpy.nan_to_num() method

numpy.nan_to_num
                (
                 x,
                 copy=True,
                 nan=0.0,
                 posinf=None,
                 neginf=None
                )

Source Code:

import numpy as np

new_arr = np.array([[np.NaN, 14, 34,np.nan], [np.NaN, np.NaN, 89,156]])
result = np.nan_to_num(new_arr)
print("Replace np.nanvalue with zero:",result)

You can refer to the below Screenshot

Python numpy replace nan with 0
Python NumPy replace nan with 0

Related NumPy tutorials:

In this Python tutorial, we learned how to delete Numpy array by using Python. Also, we will cover these topics.

  • Python numpy delete repeat
  • Python NumPy delete element
  • Python NumPy delete row
  • Python NumPy delete multiple rows
  • Python NumPy delete array
  • Python NumPy delete dimension
  • Python NumPy delete element by index
  • Python NumPy delete last column
  • Python numpy remove element by value
  • Python NumPy remove element from list
  • Python NumPy delete last element
  • Python NumPy delete zero rows
  • Python NumPy remove nan rows
  • Python numpy replace nan with 0