NumPy: Remove a specific column from a given array
Remove specific columns from a 2D array.
Write a NumPy program to remove a specific column from a given array.
Sample Solution:
Python Code:
# Importing NumPy library
import numpy as np
# Generating a random array of shape (7, 5)
nums = np.random.random((7, 5))
# Displaying the original array
print("Original array:")
print(nums)
# Deleting the first column of the array and displaying the updated array
print("\nDelete the first column of the said array:")
print(np.delete(nums, [0], axis=1))
# Deleting the last column of the array and displaying the updated array
print("\nDelete the last column of the said array:")
print(np.delete(nums, [4], axis=1))
Sample Output:
Original array: [[0.54420704 0.35710194 0.79167579 0.72249474 0.99968936] [0.22306352 0.31085825 0.09849254 0.11708716 0.45757945] [0.19381592 0.13587749 0.90455038 0.95146017 0.55716851] [0.62031347 0.84275698 0.84665943 0.06562172 0.58415968] [0.41903059 0.0660559 0.85270403 0.94184265 0.95371587] [0.02577681 0.91577282 0.1969686 0.3472482 0.23337827] [0.43563908 0.62308811 0.09606371 0.79053989 0.69382428]] Delete the first column of the said array: [[0.35710194 0.79167579 0.72249474 0.99968936] [0.31085825 0.09849254 0.11708716 0.45757945] [0.13587749 0.90455038 0.95146017 0.55716851] [0.84275698 0.84665943 0.06562172 0.58415968] [0.0660559 0.85270403 0.94184265 0.95371587] [0.91577282 0.1969686 0.3472482 0.23337827] [0.62308811 0.09606371 0.79053989 0.69382428]] Delete the last column of the said array: [[0.54420704 0.35710194 0.79167579 0.72249474] [0.22306352 0.31085825 0.09849254 0.11708716] [0.19381592 0.13587749 0.90455038 0.95146017] [0.62031347 0.84275698 0.84665943 0.06562172] [0.41903059 0.0660559 0.85270403 0.94184265] [0.02577681 0.91577282 0.1969686 0.3472482 ] [0.43563908 0.62308811 0.09606371 0.79053989]]
Explanation:
In the above code -
nums = np.random.random((7, 5)): Creates a 7x5 NumPy array with random numbers between 0 and 1.
print(np.delete(nums, [0], axis=1)) Deletes the first column (index 0) of the nums array along axis 1 (columns) and prints the resulting array.
print(np.delete(nums, [4], axis=1)) Deletes the fifth column (index 4) of the nums array along axis 1 (columns) and prints the resulting array.
For more Practice: Solve these Related Problems:
- Write a NumPy program to remove a column at a given index using np.delete and verify the new shape.
- Create a function that accepts a list of column indices to be removed from a 2D array and returns the resulting array.
- Implement a solution using boolean indexing to filter out columns where the sum is below a specified threshold.
- Test the column removal on arrays with mixed data types to ensure the function handles different dtypes correctly.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.Previous: Write a NumPy program to create an array using scientific notation numbers. Set the precision value to 6 and print the array.
Next: Write a NumPy program to create a 90x30 array filled with random point numbers, increase the number of items (10 edge elements) shown by the print statement.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics