denpy
denpy

Reputation: 279

How to create a cumulative count plot over time from a series of time range?

I have a log of people entering and leaving the room.

Sample Data:

data = {'Person':  ['A','B','C'],
        'Enter': ['11:14:22','11:15:46','11:16:22'],
        'Leave': ['11:16:40','11:18:21','11:18:22'],
        }

df = pd.DataFrame (data, columns = ['Person','Enter','Leave'])

Into:

11:13 - 0 (Empty)

11:14 - 1 (Person A only)

11:15 - 2 (A + B)

11:16 - 3 (C Enters, plus A+B)

11:17 - 2 (A left, only B + C in Room)

11:18 - 2 (Same)

11:19 - 0 (Empty)

I think I can plot by box chart (Part 2), but I am struggling to determine (or transform time-range) into how many people in the room by the minute. Any suggestions would be appreciated.

Upvotes: 0

Views: 168

Answers (1)

denpy
denpy

Reputation: 279

Essentially, I've done the following:

  1. Convert text to datetime
  2. Find min and max of time
  3. Write a for loop from min to max time (#2) at incremental of 1-minute
  4. Write a nested for loop to iterate the dataframe and see if #3 is between 'enter' and 'leave', use a counter += 1
  5. Write the results into a dictionary with key = #3 and value = counter #)

` def round_minute(atime):

   return time(atime.hour,atime.minute-1,0), time(atime.hour,atime.minute+1,0)

def headcounter(dff):

dur = (datetime.combine(datetime.today(), dff['Leave_Time'].max()) - datetime.combine(datetime.today(), dff['Join_Time'].min())).total_seconds()

time_in_session = {}
for i in range(0,math.ceil(dur/60)+3):
    _ = (datetime.combine(datetime.today(), round_minute(dff['Join_Time'].min())[0]) + timedelta(minutes = i)).time()
    counter = 0
    for z in range(0,dff.shape[0]-1):
        if (dff['Join_Time'][z] < _ and _ < dff['Leave_Time'][z]):
            counter +=1
    time_in_session[_] = counter
return time_in_session

Upvotes: 1

Related Questions