用熊猫创建特定的过滤器

安德烈·格鲁泽(Andrey Glauzer)

应用一些过滤器后,我得到以下结果。

[2 rows x 10 columns]
          id  ID_ENTIDADE                        ENTIDADE     CHAMADO     ...                 DATA_ALT VALOR_OLD           VALOR_NEW  PRIORIDADE
406  5562613          198  Professional Services > Ser...  2018015615     ...      2018-12-27 16:52:03       NaN  N1 - Security (25)           0
403  5562603          198  Professional Services > Ser...  2018015615     ...      2018-12-27 16:51:08       NaN  Contrato 629 (284)           0
405  5562606          198  Professional Services > Ser...  2018015615     ...      2018-12-27 16:51:08       3.0                   1           3
404  5562604          198  Professional Services > Ser...  2018015615     ...      2018-12-27 16:51:08       1.0                   2          14
402  5561744          198  Professional Services > Ser...  2018015615     ...      2018-12-27 16:35:06       NaN             N1 (20)           0

[5 rows x 10 columns]
          id  ID_ENTIDADE                        ENTIDADE     CHAMADO     ...                 DATA_ALT VALOR_OLD           VALOR_NEW  PRIORIDADE
408  5563214          111  Professional Services > Sup...  2018015616     ...      2018-12-27 17:02:33       NaN             N1 (20)           0
407  5563124          111  Professional Services > Sup...  2018015616     ...      2018-12-27 17:02:04       NaN  Contrato 521 (142)           0

[2 rows x 10 columns]
          id  ID_ENTIDADE                        ENTIDADE     CHAMADO     ...                 DATA_ALT VALOR_OLD           VALOR_NEW  PRIORIDADE
413  5565821          198  Professional Services > Ser...  2018015617     ...      2018-12-27 17:51:28       NaN  N1 - Security (25)           0
412  5565813          198  Professional Services > Ser...  2018015617     ...      2018-12-27 17:50:43       3.0                   1           3
411  5565809          198  Professional Services > Ser...  2018015617     ...      2018-12-27 17:50:43       1.0                   2          14
410  5565808          198  Professional Services > Ser...  2018015617     ...      2018-12-27 17:50:43       NaN  Contrato 629 (284)           0
409  5565651          198  Professional Services > Ser...  2018015617     ...      2018-12-27 17:48:01       NaN             N1 (20)           0

我的密码

df = pd.read_csv("csv.csv", sep="\t")
df2 = df.sort_values(['CHAMADO', 'id'])
g1 = df2.sort_values(['DATA_ALT'], ascending=False)
#g1 = data.groupby(['CHAMADO', 'id'])

ret_group = g1.groupby(['CHAMADO'])

for table, group in ret_group:
    print(group)

我已经制作了一个过滤器,用于按“ CHAMADO”列对项目进行分组,并根据ID列从高到低对它们进行排序。

现在,我需要过滤每个组的前3个项目,并检查“ PRIORIDADE”列中是否有值3或14

但是我找不到任何可以帮助我的东西,否则我的逻辑是错误的。

pp

现在,我需要过滤每个组的前3个项目,并检查“ PRIORIDADE”列中是否有值3或14

groupby对象给出一个可迭代的(key, dataframe)对象。因此,您可以迭代ret_group并执行检查:

for key, group in ret_group:
    test1 = group['PRIORIDADE'].eq(3).any()   # check if 3 in series
    test2 = group['PRIORIDADE'].eq(14).any()  # check if 14 in series
    tests_satisfied = test1 & test2           # check if both criteria are satisfied
    print(key, tests_satisfied)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章