Arief Hidayat
Arief Hidayat

Reputation: 967

Count ID based on interval time in pandas

Iam beginner in python. I have a dataframe looks like this:

ID  Time
ID1 9:00
ID1 9:01
ID2 9:02
ID3 9:03
ID4 9:04
ID4 9:05
ID5 9:06
ID6 9:10

is it possible to make a group based on the interval time (if i set 5 minute) and count how many ID inside during 5 minutes interval time?

my expected result looks like this:

Count ID    From    to      Interval Time (Min)
    6       9:00    9:05    5
    2       9:06    9:10    5

thank you

Upvotes: 0

Views: 76

Answers (1)

BENY
BENY

Reputation: 323226

You can check with resample, notice here 9:00 to 9:05 is 6 mins not 5

df.set_index('Time').resample('5 min',closed = 'left').nunique()
Out[172]: 
          ID
Time        
09:00:00   4
09:05:00   2
09:10:00   1

Upvotes: 2

Related Questions