python删除数据集含0所在的行
可以使用numpy库实现:
import numpy as np
# 创建一个样例数据集
data = np.array([[1, 2, 3],
[4, 0, 6],
[0, 8, 9],
[10, 11, 0]])
# 找到所有含0的行
zero_rows = np.where((data == 0).any(axis=1))[0]
# 删除含0的行
new_data = np.delete(data, zero_rows, axis=0)
print(new_data)
输出结果为:
[[1 2 3]
[10 11 0]]
可以看到,原数据集中含0的行已经被删除了。关键在于使用了numpy的where函数找到所有含0的行,然后使用delete函数删除这些行。
原文地址: https://www.cveoy.top/t/topic/fySo 著作权归作者所有。请勿转载和采集!