I am trying to find a way add a column to a dataset like this, where the value represents the lowest available slot in a queue. Each START action will use the minimum value of a sequential list, and if that list is empty it will use the next number in the sequence. Each END action will return that associated value back to the list.
ACTION TIME PID
0 START 12:01 111
1 FINISH 12:02 111
2 START 12:03 222
3 START 12:04 333
4 START 12:05 444
5 FINISH 12:06 333
6 START 12:05 555
7 FINISH 12:07 444
8 FINISH 12:08 222
9 FINISH 12:08 555
10 START 12:20 777
...
The expected result would look something like this. To the right of the bar is my current solution using iterrows(), maintaining a list of the available spots, and the max queue level used so far.
ACTION TIME PID QUEUE | (NEXT) (MAX)
0 START 12:01 111 0 | [] 0
1 FINISH 12:02 111 0 | [0] 0
2 START 12:03 222 0 | [] 0
3 START 12:04 333 1 | [] 1
4 START 12:05 444 2 | [] 2
5 FINISH 12:06 333 1 | [1] 2
6 START 12:05 555 1 | [] 2
7 FINISH 12:07 444 2 | [2] 2
8 FINISH 12:08 222 0 | [2,0] 2
9 FINISH 12:08 555 1 | [2,0,1] 2
10 START 12:20 777 0 | [2,1] 2
...
# Current iterative solution
next_queue = [0]
max_queue = 0
for i,r in df.iterrows():
if r['ACTION']=='START':
if len(next_queue)==0:
max_queue = max_queue + 1
next_queue.append(max_queue)
row_queue = min(next_queue)
next_queue.remove(row_queue)
df.loc[df['PID']==r['PID'],'QUEUE']=row_queue
continue
return_queue = df.loc[df.index=i,'QUEUE']
next_queue.append(return_queue)
Is there a better way, maybe vectorized way, to do this? Also, is there a name for this minimum value queue? (Edit: is this an ascending priority queue?)