如何从Numpy数组中删除最后N行?
在本文中,我们将讨论如何从 NumPy 数组中删除最后 N 行。
方法一:使用切片运算符
切片是一种索引操作,用于迭代数组。
Syntax: array_name[start:stop]
where start is the start is the index and stop is the last index.
我们也可以在Python进行负切片。它由以下语法表示。
Syntax: array_name[: -n]
where, n is the number of rows from last to be deleted.
示例 1:
我们将创建一个 6 行 3 列的数组,并使用切片删除最后 N 行。
Python3
# importing numpy module
import numpy as np
# create an array with 6 rows and 3 columns
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9],
[10, 11, 12], [13, 14, 15], [16, 17, 18]])
print(a)
# delete last 1 st row
print("data after deleting last one row ", a[:-1])
# delete last 2 nd row
print("data after deleting last two rows ", a[:-2])
# delete last 3 rd row
print("data after deleting last theww rows ", a[:-3])
# delete last 4 th row
print("data after deleting last four rows ", a[:-4])
# delete last 5 th row
print("data after deleting last five rows ", a[:-5])
# delete last 6 th row
print("data after deleting last six rows ", a[:-6])
Python3
# importing numpy module
import numpy as np
# create an array with 5 rows and
# 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# use for loop to iterate over the
# elements
for i in range(1, len(a)+1):
print("Iteration No", i, "deleted", i, "Rows")
print("Remaining data present in the array is\n ", a[:-i])
Python3
# importing numpy module
import numpy as np
# create an array with 5 rows and
# 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# place first 2 rows in b variable
# using slice operator
b = a[:2]
print(b)
Python3
# importing numpy module
import numpy as np
# create an array with 5 rows and
# 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# delete last three rows
# using numpy.delete
a = np.delete(a, [2, 3, 4], 0)
print(a)
Python3
# importing numpy module
import numpy as np
# create an array with 5 rows and 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# delete last three rows
# using numpy.delete
a = np.delete(a, [0, 1, 2, 3, 4], 0)
print(a)
输出:
示例 2:
我们使用 for 循环遍历元素并使用切片运算符,我们将删除数据然后打印数据。
蟒蛇3
# importing numpy module
import numpy as np
# create an array with 5 rows and
# 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# use for loop to iterate over the
# elements
for i in range(1, len(a)+1):
print("Iteration No", i, "deleted", i, "Rows")
print("Remaining data present in the array is\n ", a[:-i])
输出:
示例 3:
我们还可以指定我们需要的元素,并使用切片运算符将它们存储到另一个数组变量中。这样,我们不会得到最后 N 行(删除那些)。
蟒蛇3
# importing numpy module
import numpy as np
# create an array with 5 rows and
# 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# place first 2 rows in b variable
# using slice operator
b = a[:2]
print(b)
输出:
[[21 7 8 9]
[34 10 11 12]]
方法二:使用numpy.delete()方法
它用于根据行号删除 NumPy 数组中的元素。
Syntax: numpy.delete(array_name,[rownumber1,rownumber2,.,rownumber n],axis)
Parameters:
- array_name is the name of the array.
- row numbers is the row values
- axis specifies row or column
- axis=0 specifies row
- axis=1 specifies column
在这里,我们将删除最后一行,因此在列表中指定行号。
示例 1:删除最后三行
蟒蛇3
# importing numpy module
import numpy as np
# create an array with 5 rows and
# 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# delete last three rows
# using numpy.delete
a = np.delete(a, [2, 3, 4], 0)
print(a)
输出:
[[21 7 8 9]
[34 10 11 12]]
示例 2:删除所有行
蟒蛇3
# importing numpy module
import numpy as np
# create an array with 5 rows and 4 columns
a = np.array([[21, 7, 8, 9], [34, 10, 11, 12],
[1, 3, 14, 15], [1, 6, 17, 18],
[4, 5, 6, 7]])
# delete last three rows
# using numpy.delete
a = np.delete(a, [0, 1, 2, 3, 4], 0)
print(a)
输出:
[ ]