python - 检查何时多个数据框列在 Python 中具有特定值
问题描述
我的数据格式如下:
Shop Date Produced Lost Output Signal
Cornerstop 01-01-2010 0 1 9 1
Cornerstop 01-01-2010 11 1 11 0
Cornerstop 01-01-2010 0 0 0 2
Cornerstop 01-01-2010 1 0 0 2
Cornerstop 01-01-2010 5 7 0 2
.
.
.
.
当'Produced'为 0 时,数据的'Lost'和'Output'值应该为 0,但事实并非如此。我需要一种方法来找出何时不是这种情况(当 Produced 为 0 但 Lost、Output 或 Signal 中的任何一个都不是 0 时)。
制作一个计数器来计算这是真的与否的次数是我以前看到的数字:
counter = 0
for index, row in data.iterrows():
if row['Produced'] and row['Lost'] != 0:
counter += 1
else:
continue
我想确切地查看这些数据框中的哪些行(这是一个很大的集合),而且按每一行搜索几乎不是很有效。
有没有更好的方法可以做到这一点?
解决方案
尝试布尔索引:
data[(data['Produced'] == 0) & (data['Lost'] != 0) & (data['Output'] != 0) & (data['Signal'] != 0)]
推荐阅读
- javascript - SlideToggle:可见 - 多个切换的问题
- php - 当类启动失败时,我可以返回 null 而不是对象吗?
- reactjs - 如何构建 React 应用程序并将 URL 从构建外部的文件获取到后端?
- angular - Angular 11 从 LinkedIn 未知错误获取 /v2/me
- android - Flutter better_player 无法播放没有扩展名的视频 url
- r - 如何通过 R 中的 3D 数据绘制曲面拟合?
- security - PasswordCheckFailedException: CWWIM4537E 未从“Client01”主体名称中找到主体
- json - 如何从 yaml 文件中读取字典中需要在 helm 中使用的值
- amazon-cognito - AWS Cognito 更改电子邮件 - 更新前需要验证
- firebase - Flutter 页面刷新问题 | ListView 显示错误“BOTTOM OVERFLOWED BY 98 PIXEL”