title: Reconstruct L2 order book dataframe from deltas dataframe
body: <p>I have <code>deltas</code> dataframe where each row with <code>delta==True</code> represents an orderbook update (meaning for given <code>price</code> we update <code>size</code>). If <code>size</code> is 0.0 price level should be removed from orderbook and all price levels above (below for bids) should be shifted.</p>
  <pre><code>         timestamp  receipt_timestamp delta side   price      size  pos
  0     1.626161e+09       1.626161e+09  True  bid  1.3125   31626.0    0
  1     1.626161e+09       1.626161e+09  True  bid  1.3124   45571.0    1
  2     1.626161e+09       1.626161e+09  True  bid  1.3123   57483.0    2
  3     1.626161e+09       1.626161e+09  True  bid  1.3122  184117.0    3
  4     1.626161e+09       1.626161e+09  True  bid  1.3128    2202.0    4
  ...            ...                ...   ...  ...     ...       ...  ...
  2585  1.626163e+09       1.626163e+09  True  ask  1.3174   45426.0    9
  2586  1.626163e+09       1.626163e+09  True  ask  1.3212   54472.0   10
  2587  1.626163e+09       1.626163e+09  True  ask  1.3150   10102.0   11
  2588  1.626163e+09       1.626163e+09  True  ask  1.3173   62527.0   12
  2589  1.626163e+09       1.626163e+09  True  ask  1.3165  124279.0   13
  </code></pre>
  <p><code>delta==False</code> represents a whole orderbook snapshot, where, if grouped by <code>timestamp</code>, each row represents <code>size</code> for each <code>price</code> level (each group size is the same for each <code>timestamp</code>), prices should be sorted within groups accordingly.</p>
  <p>What I'm trying to get is a dataframe where each row is a full orderbook state for each timestamp, something similar to:</p>
  <pre><code>               price                                                          \
  side             bid                                                           
  pos               0       1       2       3       4       5       6       7    
  timestamp                                                                      
  1.626161e+09  1.3123  1.3122  1.3121  1.3120  1.3119  1.3118  1.3117  1.3116   
  1.626162e+09  1.3137  1.3136  1.3135  1.3134  1.3133  1.3132  1.3131  1.3130   
  
                                ...      size                              \
  side                          ...       ask                               
  pos               8       9   ...        90       91        92       93   
  timestamp                     ...                                         
  1.626161e+09  1.3115  1.3114  ...    4845.0  30704.0   35843.0   1806.0   
  1.626162e+09  1.3129  1.3128  ...   24541.0  79523.0  124115.0  60384.0   
  
  [11 rows x 400 columns]
  </code></pre>
  <p>Basically if we have 200 price levels (100 bids and 100 asks), I'll need 400 columns total: 100 ask prices, 100 bid prices, 100 ask sizes, 100 bid sizes, values should be sorted for columns (representing orderbook level).</p>
  <p>The question is, how do I do this without iterating each row in <code>deltas</code> df?</p>
  <p>What I did so far is reconstructing snapshots only (<code>delta==False</code>) using <code>groupby</code> <code>timestamp</code> with <code>cumcount</code> (to get <code>pos</code> column, meaning price level position). However what I dont get is how to update it when <code>delta==True</code>, specifically shifting values within one row if price level is cleared. Any thoughts?</p>