I have the below sql query,
count(ticket_id) from interactions as intr
where ticket_status='PENDING'
I need to acheive the same in python. I tried the below approach. Is anything could be better than this?
subset_df = df[df["ticket_status"]== 'PENDING']
column_count = subset_df.count()
This will give me the count of status column only.
But I'm trying to get the ticket id with that where condition.
I want to count the ticket id with that particular status. Any help would be appreciatable.
CodePudding user response:
What works for me is: df[df["ticket_status"]== 'PENDING'].value_counts().count()
This will give the number of tickets that have a ticket_status of 'PENDING'.
CodePudding user response:
If your goal is to count all distinct instances in a column you can use
df['ticket_status'].value_counts()
For counting values with conditional you can use
df.where(df['ticket_status'] == 'myvalue').count()
A helpful guide for you to compare your queries from SQL to pandas would be https://pandas.pydata.org/docs/getting_started/comparison/comparison_with_sql.html
CodePudding user response:
Try this:
df.ticket_status.value_counts()