Python - Numpy Array Column Deletion


In this problem statement we have to perform a deletion operation for deleting the column using the numpy array in Python. Sometimes we need to delete some data from the datasets so that time this problem can be helpful to solve it.

Understanding the Problem

Numpy library is very useful in data manipulation and numerical calculations. So deleting the column from an array is a very common task. In this problem we will be using the numpy array and delete one column and show the remaining data on the console. So the multidimensional array will be used in this problem.

Logic for The Above Problem

To address the aforementioned given problem of deleting a column from a Numpy array. As the Numpy arrays are immutable in nature so we are not able to remove them directly. We will create a new array and store the remaining columns after removing the desired column. So we will use different functions of Numpy to remove the desired column.

Algorithm

  • Step 1 − As we are using Numpy arrays in our code so first we are required to import the numpy library as nmp.

  • Step 2 − And then we will initiate a function called column_delete and pass two parameters as arr and column_index. The arr will be the input multidimensional array and column_index will be the index of the column which is to be deleted.

  • Step 3 − Then using the delete function of the numpy library and passing the axis of the index we are passing 1 in our case because we have to remove or delete the column.

  • Step 4 − Initiate the numpy array as numpy_array. And we are using nmp.array to convert the array as a numpy array.

  • Step 5 − Now we will call the above created function and pass the numpy array and column index which we want to delete.

Example

#Import numpy library as nmp
import numpy as nmp

def column_delete(arr, column_index):
   return nmp.delete(arr, column_index, axis=1)

# Initialize the numpy array
numpy_array = nmp.array([['Cyan', 'Red', 'Black'],
               ['Peach', 'Blue', 'Pink'],
               ['Green', 'White', 'Grey']])
               
# Call the function to delete the second column
new_array = column_delete(numpy_array, 1)
print("Before Deletion: \n", numpy_array)
print("\nAfter Deletion \n",new_array)

Output

Before Deletion:
[['Cyan' 'Red' 'Black']
 ['Peach' 'Blue' 'Pink']
 ['Green' 'White' 'Grey']]

After Deletion
 [['Cyan' 'Black']
 ['Peach' 'Pink']
 ['Green' 'Grey']]

Complexity

The time complexity for this problem os deleting the Numpy array column is O(m * n). In this m is the count of rows and n is the counts of columns in the multidimensional array. As we are excluding the column we are deleting from the given input array and creating a new array.

Conclusion

In this article we have successfully implemented the code for deleting a column from a numpy array using the functions and methods of the Numpy Python library.

Updated on: 18-Oct-2023

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements