📜  Python – Numpy 数组列删除

📅  最后修改于: 2022-05-13 01:55:11.560000             🧑  作者: Mango

Python – Numpy 数组列删除

给定一个 numpy 数组,编写一个程序从 numpy 数组中删除列。

例子 -

Input: [['akshat', 'nikhil'], ['manjeeet', 'akash']]
Output:  [['akshat']['manjeeet']]

Input:  [[1, 0, 0, 1, 0], [0, 1, 2, 1, 1]]
Output:  [[1 0 1 0][0 2 1 1]]

下面给出了从 numpy 数组中删除列的各种方法。

方法 #1:使用 np.delete()

# Python code to demonstrate
# deletion of columns from numpy array
  
import numpy as np
  
# initialising numpy array
ini_array = np.array([[1, 0, 0, 1, 0],
                        [0, 1, 2, 1, 1]])
                          
  
# deleting second column from array
result = np.delete(ini_array, 1, 1)
  
# print result
print ("Resultant Array :"+str(result))

输出:

Resultant Array :[[1 0 1 0]
 [0 2 1 1]]

方法 #2:使用 compress() 和 logical_not()

# Python code to demonstrate
# deletion of columns from numpy array
  
import numpy as np
  
# initialising numpy array
ini_array = np.array([[1, 0, 0, 1, 0], [1, 2, 0, 0, 1]])
z = [False, True, False, False, False]
                          
  
# deleting second column from array
result = ini_array.compress(np.logical_not(z), axis = 1)
  
# print result
print ("Resultant Array :"+str(result))

输出:

Resultant Array :[[1 0 1 0]
 [1 0 0 1]]

方法#3:使用logical_not()

# Python code to demonstrate
# deletion of columns from numpy array
  
import numpy as np
  
# initialising numpy array
ini_array = np.array([[1, 0, 0, 1, 0], [1, 2, 0, 0, 1]])
z = [False, True, False, False, False]
                          
  
# deleting second column from array
result = ini_array[:, np.logical_not(z)]
  
# print result
print ("Resultant Array :"+str(result))

输出:

Resultant Array :[[1 0 1 0]
 [1 0 0 1]]