python - 删除数组的特定值:Python
问题描述
我有一个形状数组(1179648, 909)
。问题是某些行仅填充了0
'。我正在检查如下:
for i in range(spectra1Only.shape[0]):
for j in range(spectra1Only.shape[1]):
if spectra1Only[i,j] == 0:
我现在想删除整行,[i]
如果有任何0
似乎只获得少量所需的数据。
我的问题是:最好的方法是什么?Remove
? Del
? numpy.delete
? 还是有什么其他方法?
解决方案
您可以将布尔索引与np.any
along 一起使用axis=1
:
spectra1Only = spectra1Only[~(spectra1Only == 0).any(1)]
这是一个演示:
A = np.random.randint(0, 9, (5, 5))
print(A)
[[5 0 3 3 7]
[3 5 2 4 7]
[6 8 8 1 6]
[7 7 8 1 5]
[8 4 3 0 3]]
print(A[~(A == 0).any(1)])
[[3 5 2 4 7]
[6 8 8 1 6]
[7 7 8 1 5]]
推荐阅读
- python - Python 与元组集的传递关系
- java - 如何在 java 中获取失败的 HTTP 请求正文?
- javascript - 在我的 reactjs 中,我最初使用了调色板马赛克字体。我将字体更改为建筑师女儿,但它没有改变。问题是什么?
- php - 我在搜索中嵌入了来自 youtube 的视频,我想在 ajax 的帮助下显示视频而不重新加载页面
- node.js - 将 Promise 从后台进程返回到渲染器进程是否有效?
- mongodb - 通过索引从对象数组中修改对象并将新对象插入mongodb的同一数组中的有效方法?
- angularjs - 如何从浏览器中的 SAML 响应中获取访问令牌?
- ruby - 在 Windows 中安装 zxing_cpp gem 时 cmake 抛出错误
- oracle - obiee12c RCU 创建失败,Oracle 数据库 19c
- intellij-idea - IntelliJ 在同一个包中显示源文件和测试文件