title: Efficiently repeating pandas dataframe rows based on log data column
body: <p>I'm working with log data for a classification problem. Each row consists of a given session's action and each session is divided into subsessions. My objective is to repeat previous subsession's actions in given subsessions (retrieving the last N) in a given session. I'm having trouble coming up with time efficient solutions for my 4M+ row dataset.</p>
  
  <p><strong>Simplified example</strong></p>
  
  <p>Note - Other features are omitted, <code>action</code> corresponds to the subsession action index and <code>orig_action</code> is just a helper for the example, where <code>A2.1</code> corresponds to action 1 from the original subsession A2, <code>A3.1</code> corresponds to action 1 from the original subsession A3, and so on.</p>
  
  <pre><code># - Original dataframe
  
    session  subsession  action
  0       A           1       1
  1       A           2       1
  2       A           2       2
  3       A           3       1
  4       B           1       1
  
  # - Expanded dataframe
  # Session A:
  # Subsession 2 now encompasses last N actions from subsessions 1 and 2
  # Subsession 3 now encompasses last N actions from subsessions 1, 2 and 3
  # Check above note for orig_action explanation.
  
    session  subsession  action  orig_action
  0       A           1       1         A1.1
  1       A           2       1         A1.1
  2       A           2       2         A2.1
  3       A           2       3         A2.2
  4       A           3       1         A1.1
  5       A           3       2         A2.1
  6       A           3       3         A2.2
  7       A           3       4         A3.1
  8       B           1       1         B1.1
  </code></pre>
  
  <p>The number of subsessions can reach up to 500.</p>
  
  <p>Does anyone have any idea of how I might be able to do this without it taking days with an iterative loop? </p>