chink
chink

Reputation: 1643

How to use groupby and calculate the counts for each group

I have a dataframe which looks like this

DateTime      RunStatus hour
2018-05-08 01:45:00 0.0 1
2018-05-08 02:00:00 0.0 2
2018-05-08 02:15:00 0.0 2
2018-05-08 02:30:00 0.0 2
2018-05-08 02:45:00 0.0 2
2018-05-08 03:00:00 1.0 3
2018-05-08 03:15:00 1.0 3
2018-05-08 03:30:00 0.0 3
2018-05-08 07:45:00 0.0 7
2018-05-08 08:00:00 0.0 8
2018-05-08 08:15:00 0.0 8
2018-05-08 08:30:00 0.0 8
2018-05-08 08:45:00 0.0 8
2018-05-08 09:00:00 1.0 9
2018-05-08 09:15:00 1.0 9
2018-05-08 09:30:00 1.0 9
2018-05-08 09:45:00 0.0 9
2018-05-08 10:00:00 0.0 10
2018-05-08 10:15:00 0.0 10
2018-05-08 10:30:00 0.0 10
2018-05-08 10:45:00 0.0 10
2018-05-08 11:00:00 0.0 11
2018-05-08 11:15:00 0.0 11
2018-05-08 11:30:00 0.0 11
2018-05-08 11:45:00 0.0 11
2018-05-08 12:00:00 0.0 12
2018-05-08 12:15:00 1.0 12
2018-05-08 12:30:00 1.0 12
2018-05-08 12:45:00 1.0 12

I would like to group using hours variable and for each hour want the count of number of times runstatus is 0 and runstatus is 1

Upvotes: 0

Views: 40

Answers (2)

jDoe
jDoe

Reputation: 1023

Assuming your dataframe is is df:

runStatusCount = df.drop('date',axis=1)[df['RunStatus'].isin([0,1])].groupby('hour').count()

Upvotes: 0

BENY
BENY

Reputation: 323226

Using crosstab after modify the format of your Datetime

pd.crosstab(df.DateTime.dt.strftime('%Y-%m-%d %H'),df.RunStatus)

Upvotes: 2

Related Questions