Filter count in pandas

52 views Asked by At

I have been coding extensively for years using DAX and trying to move to pandas. I am used to creating generic measures and then filtering them to answer specific questions. So I am working with a ticket dataset. I want to know the number of open tickets. In Dax, I would create a generic measure for Ticket Count which I can reuse and for open tickets I would reuse the Ticket Count measure but filter for tickets with status=="Open".

Here is what I did:

countTickets=incidentManagement['TicketId'].count()
##gives Ticket Count (works perfectly)
numOpentickets=incidentManagement[incidentManagement.Status=="Open"].incidentManagement['TicketId'].count()
print(numOpentickets)

I am unable to make the next line of code work. I want to resuse countTickets and simply filter it. There are several examples of filtering the entire dataframe but I was wondering if I could reuse the variable I have already created called countTickets.

1

There are 1 answers

0
ansev On BEST ANSWER

Use DataFrame.loc:

numOpentickets=incidenteManagement.loc[incidenteManagement.Status == "Open",'TicketId'].count()
print(numOpentickets)