D_M
D_M

Reputation: 87

Count number of rows in dataframe to determine number of events at time and date?

I am trying to determine the number of times a certain event occurred within a timeframe.

For the following data: Date | Time (in 24 hour format) | didEventHappen 1/1/15 1130 Yes 1/1/15 1145 Yes 1/1/15 1215 Yes 1/2/15 1030 Yes 1/2/15 1145 Yes 1/2/15 1015 Yes I want the output to be:

Date | Time (hour in 24 hour format)| EventCount 1/1/15 11 2 1/1/15 12 1 1/2/15 10 2 1/2/15 11 1

Upvotes: 0

Views: 49

Answers (1)

BENY
BENY

Reputation: 323226

You can check groupby with str slice

df['didEventHappen'].eq('Yes').groupby([df.Date,df.Time.astype(str).str[:2]]).sum().reset_index()
Out[62]: 
     Date Time  didEventHappen
0  1/1/15   11             2.0
1  1/1/15   12             1.0
2  1/2/15   10             2.0
3  1/2/15   11             1.0

Upvotes: 1

Related Questions