应用一些过滤器后,我得到以下结果。
[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
但是我找不到任何可以帮助我的东西,或者我的逻辑是错误的。
答案 0 :(得分:1)
现在,我需要过滤每个组的前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)
答案 1 :(得分:0)
尝试做:
ret_group = g1.groupby(['CHAMADO'])
filter_group = ret_group [ret_group['PRIORIDADE'] == 3]
如果这可行,那么您可以这样做:
filter_group = ret_group [(ret_group['PRIORIDADE'] == 3) & (ret_group['PRIORIDADE'] == 14) ]
或者您可以这样做:
df_filtered = g1.groupby(['CHAMADO'])['PRIORIDADE']
df_filtered = df_filtered.to_frame()
df_prioridade3 = df_filtered[df_filtered['PRIORIDADE'] == 3]
df_prioridade14 = df_filtered[df_filtered['PRIORIDADE'] == 14]
然后检查df_prioridade3
和df_prioridade14
是否不是空