在 NumPy 中删除行
Muhammad Maisam Abbas
2021年7月4日

本教程将介绍如何在 Python 中从多维 NumPy 数组中删除一行。
使用 numpy.delete()
函数删除 NumPy 行
如果我们有一个多维 NumPy 数组并想从中删除特定行,我们可以使用 numpy.delete()
函数。numpy.delete()
函数 从 NumPy 数组中沿指定轴删除特定索引处的条目。numpy.delete()
函数将数组、要删除的索引和要删除的轴作为参数,并返回一个子数组,其中删除了指定索引和指定轴。下面的代码示例向我们展示了如何使用 numpy.delete()
函数从多维数组中删除一行。
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array = np.delete(array, (1), axis=0)
print(array)
输出:
[[1 2 3]
[7 8 9]]
我们使用上面代码中的 np.delete()
函数从二维 NumPy 数组 array
中删除了第二行。我们首先使用 np.array()
函数创建了二维 NumPy 数组 array
。然后我们沿着轴 0
删除索引 1
处的条目,这是 array
的第二行,并将结果保存在 array
中。
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn