5
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.

Programmer
  • 1,266
  • 5
  • 23
  • 44

4 Answers4

7

Let us do something different

pd.crosstab(df.Month,df.Status)['X']
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 1
    me to! @Andy L. – ansev Nov 07 '19 at 02:06
  • 2
    @ansev: I like Wen's different things. He usually comes up with surprisingly refreshed solution which I never think of. I still remember his solution using `sum` to `concatenate` series of lists which I never thought of before :) – Andy L. Nov 07 '19 at 02:17
6

A short way

(df.Status == 'X').groupby(df.Month).sum()

A long way

df.where(df.Status == 'X').groupby('Month').Status.count()
Andy L.
  • 24,909
  • 4
  • 17
  • 29
6

Also can use lambda function

df.groupby('Month').agg(lambda x: (x=='X').sum())

or

df.groupby('Month').Status.agg(lambda x: (x=='X').sum())
ansev
  • 30,322
  • 5
  • 17
  • 31
1

You an do df.loc[df.Status=='X'].groupby(['Month']).agg({'Status' : ['count']})

moys
  • 7,747
  • 2
  • 11
  • 42