📜  删除 NumPy ndarray 的行和列

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

删除 NumPy ndarray 的行和列

在本文中,我们将讨论如何删除 n 维数组中的指定行和列。我们将使用 numpy.delete()方法删除行和列。

让我们借助一些例子来讨论:

示例 1:

用NumPy创建一个二维数组(3行4列)并删除指定行的程序。



Python3
# importing numpy module
import numpy as np
  
# create an array with integers
# with 3 rows and 4 columns
a = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8], 
              [9, 10, 11, 12]])
print(a)
  
# delete 0 th row
data = np.delete(a, 0, 0)
print("data  after 0 th row deleted :", data)
  
# delete 1 st row
data = np.delete(a, 1, 0)
print("data  after 1 st  row deleted :", data)
  
# delete 2 nd row
data = np.delete(a, 2, 0)
print("data  after 2 nd  row deleted :", data)


Python3
# importing numpy module
import numpy as np
  
# create an array with integers with
# 6 rows and 2 columns
a = np.array([[1, 2], [5, 6], [9, 10, ],
              [78, 90], [4, 89], [56, 43]])
print(a)
  
# delete 0 th column
data = np.delete(a, 0, 1)
print("data  after 0 th  column  deleted :", data)
  
# delete 1 st column
data = np.delete(a, 1, 1)
print("data  after 1 st  column  deleted :", data)


Python3
# importing numpy module
import numpy as np
  
# create an array with integers
# with 3 rows and 3 columns
a = np.array([[67, 65, 45], 
              [45, 67, 43], 
              [3, 4, 5]])
print("Original\n", a)
  
# delete 1 st row
data = np.delete(a, 0, 0)
print("data  after 1 st row   deleted :\n", data)
  
# delete 1 st column
data = np.delete(a, 0, 1)
print("data  after 1 st  column  deleted :\n", data)


Python3
# importing numpy module
import numpy as np
  
# create an array with integers 
# with 3 rows and 3 columns
a = np.array([[67, 65, 45], 
              [45, 67, 43], 
              [3, 4, 5]])
print("Original\n", a)
  
# delete 1 st row and 2 nd 
# row at a time
data = np.delete(a, [0, 1], 0)
print("data  after 1 st  and 2 ns row deleted :\n", data)


Python3
# importing numpy module
import numpy as np
  
# create an array with integers 
# with 3 rows and 3 columns
a = np.array([[67, 65, 45],
              [45, 67, 43], 
              [3, 4, 5]])
print("Original\n", a)
  
  
# delete 1 st column and 3 rd 
# column at a time
data = np.delete(a, [0, 2], 1)
print("data  after 1 st and 3 rd column  deleted :\n", data)


输出:

示例 2:

程序使用 NumPy 创建一个二维数组(6 行 2 列)并删除指定的列。

蟒蛇3

# importing numpy module
import numpy as np
  
# create an array with integers with
# 6 rows and 2 columns
a = np.array([[1, 2], [5, 6], [9, 10, ],
              [78, 90], [4, 89], [56, 43]])
print(a)
  
# delete 0 th column
data = np.delete(a, 0, 1)
print("data  after 0 th  column  deleted :", data)
  
# delete 1 st column
data = np.delete(a, 1, 1)
print("data  after 1 st  column  deleted :", data)

输出:



示例 3:

删除 1 行和 1 列。

蟒蛇3

# importing numpy module
import numpy as np
  
# create an array with integers
# with 3 rows and 3 columns
a = np.array([[67, 65, 45], 
              [45, 67, 43], 
              [3, 4, 5]])
print("Original\n", a)
  
# delete 1 st row
data = np.delete(a, 0, 0)
print("data  after 1 st row   deleted :\n", data)
  
# delete 1 st column
data = np.delete(a, 0, 1)
print("data  after 1 st  column  deleted :\n", data)

输出:

示例 4:

通过在 obj 参数中将行号作为列表传递,我们可以一次删除 n 行。

蟒蛇3

# importing numpy module
import numpy as np
  
# create an array with integers 
# with 3 rows and 3 columns
a = np.array([[67, 65, 45], 
              [45, 67, 43], 
              [3, 4, 5]])
print("Original\n", a)
  
# delete 1 st row and 2 nd 
# row at a time
data = np.delete(a, [0, 1], 0)
print("data  after 1 st  and 2 ns row deleted :\n", data)

输出:



示例 5:

通过在 obj 参数中将列号作为列表传递,我们可以一次删除 n 个列。

蟒蛇3

# importing numpy module
import numpy as np
  
# create an array with integers 
# with 3 rows and 3 columns
a = np.array([[67, 65, 45],
              [45, 67, 43], 
              [3, 4, 5]])
print("Original\n", a)
  
  
# delete 1 st column and 3 rd 
# column at a time
data = np.delete(a, [0, 2], 1)
print("data  after 1 st and 3 rd column  deleted :\n", data)

输出: