首页 > 解决方案 > 检查何时多个数据框列在 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

我想确切地查看这些数据框中的哪些行(这是一个很大的集合),而且按每一行搜索几乎不是很有效。

有没有更好的方法可以做到这一点?

标签: pythonpython-3.xpandasdataframeif-statement

解决方案


尝试布尔索引

data[(data['Produced'] == 0) & (data['Lost'] != 0) & (data['Output'] != 0) & (data['Signal'] != 0)]

推荐阅读