user5652202
user5652202

Reputation:

Create specific filters with pandas

I have the following result after applying some filters.

[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

My code

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)

I've already made a filter that groups the items by the "CHAMADO" column and sorts them from highest to least according to the ID column.

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"

But I'm not finding anything that can help me, or my logic is wrong.

Upvotes: 1

Views: 42

Answers (2)

jalazbe
jalazbe

Reputation: 2005

Try doing:

ret_group = g1.groupby(['CHAMADO'])
filter_group = ret_group [ret_group['PRIORIDADE'] == 3]

If this works then you may do:

filter_group = ret_group [(ret_group['PRIORIDADE'] == 3) & (ret_group['PRIORIDADE'] == 14) ]

Or you could do:

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]

And then check if df_prioridade3 and df_prioridade14 are not empy

Upvotes: 0

jpp
jpp

Reputation: 164673

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 objects give an iterable of (key, dataframe) objects. So you can iterate the ret_group and perform your checks:

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)

Upvotes: 1

Related Questions