Home > Software design >  Pandas count by time slots
Pandas count by time slots

Time:10-03

I'm new to pandas and it's still hard to understand how to do that. In pure python, it becomes so hard and unreadable. I need to count rows day-by-day with 1-hour time slots (is the begin-end range in this slot).

For ex., for data:

    begin_time              end_time
2020-01-01 11:02:10     2020-01-01 12:33:05
2020-01-01 12:22:20     2020-01-01 13:01:51
2020-01-02 09:02:24     2020-01-02 11:33:46

we'll have:

# for 2020-01-01
  time slot      count
11:00 - 12:00      1
12:00 - 13:00      2
13:00 - 14:00      1
...

Help will be gladly accepted. Thanks.

CodePudding user response:

This question needs a bit more context but I think you're looking for

df.groupby([pd.Grouper(key='begin_time', freq='H')])['column_to_count'].count()
  • Related