根据多列在熊猫数据框中查找模式

时间:2020-04-26 17:38:37

标签: python pandas pandas-groupby

我有如下数据

Equipment   Timestamp           col       value
D1          18/04/2020 23:59    Command     1
            18/04/2020 23:59    Run_status  1
            19/04/2020 23:59    Run_status  0
            21/04/2020 00:59    Command     1
            22/04/2020 01:09    Command     1

我需要找到以下模式:

d ['col'] ='Command'和d ['col']。shift()='Run_status'

AND d ['value'] = 1&d ['value']。shift()= 1

AND(d ['Timestamp']-d ['Timestamp']。shift())

然后创建一个新列,当找到这种模式时将给出True:

Equipment   Timestamp           col          value  New_col
D1          18/04/2020 23:59    Command        1    TRUE
            18/04/2020 23:59    Run_status     1    FALSE
            19/04/2020 23:59    Run_status     0    FALSE
            21/04/2020 00:59    Command        1    FALSE
            22/04/2020 01:09    Command        1    FALSE

如何创建可以找到所需模式的New_col?

1 个答案:

答案 0 :(得分:1)

Pandas中的条件会生成布尔数组。您可以将它们与二进制&|运算符结合使用。要添加新列,只需为其分配。

cond1 = (d['col'] == 'Command') & (d['col'].shift(-1) == 'Run_status')
cond2 = (d['value'] == 1) & (d['value'].shift(-1) == 1)
cond3 = (d['Timestamp'].shift(-1) - d['Timestamp']) < timedelta(minutes=5)
d['New_col'] = cond1 & cond2 & cond3
相关问题