Pandas groupby and agg by condition
Asked Answered
C

4

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.

Consumable answered 7/11, 2019 at 1:26 Comment(3)
Please add some sample data in text format for us to to checkKeenankeene
Have you tried .filter method. I can't comment much without the sample dataKeenankeene
#27488580 , have you checked this link. It explains how to use .apply to do what you want.Keenankeene
H
6

A short way

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

A long way

df.where(df.Status == 'X').groupby('Month').Status.count()
Hematuria answered 7/11, 2019 at 1:36 Comment(0)
R
7

Let us do something different

pd.crosstab(df.Month,df.Status)['X']
Reinstate answered 7/11, 2019 at 1:50 Comment(2)
me to! @Andy L.Undine
@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 :)Hematuria
U
7

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())
Undine answered 7/11, 2019 at 2:29 Comment(0)
H
6

A short way

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

A long way

df.where(df.Status == 'X').groupby('Month').Status.count()
Hematuria answered 7/11, 2019 at 1:36 Comment(0)
J
1

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

Jankell answered 7/11, 2019 at 1:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.