gpt4 book ai didi

python - 使用 pandas 创建特定过滤器

转载 作者:行者123 更新时间:2023-12-01 08:26:50 24 4
gpt4 key购买 nike

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

[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

但是我没有找到任何可以帮助我的东西,或者我的逻辑是错误的。

最佳答案

Now I would need to filter the first 3 items of each group and check if there are values 3 or 14 in the column "PRIORIDADE"

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)

关于python - 使用 pandas 创建特定过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54181637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com