df.groupby(['Month']).agg({'Status' : ['count']})
The line above groups the dataframe by Month
and counts the number of Status
for each month. Is there a way to only get a count where Status=X
? Something like the incorrect code below:
df.groupby(['Month']).agg({'Status' == 'X' : ['count']})
Essentially, I want a count of how many Status
are X
for each month.
A short way
(df.Status == 'X').groupby(df.Month).sum()
A long way
df.where(df.Status == 'X').groupby('Month').Status.count()
Let us do something different
pd.crosstab(df.Month,df.Status)['X']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With