title: How to insert rows depending on previous row meeting conditions
body: <p>I have a simplified file (foo.csv),</p>
  <p><strong>Contents of foo:</strong></p>
  <pre><code>['MyNum', 'Cycle', 'Line', 'V1', 'V2', 'T1']
  ['1', 'C', '1', '6.7', '25.6', '90']
  ['3', 'A', '1', '5.8', '22.5', '89.9']
  ['3', 'A', '2', '5.8', '24.2', '90']
  ['3', 'A', '3', '5.8', '25.4', '90']
  ['5', 'B', '1', '6', '25.3', '89.9']
  ['5', 'B', '2', '6.3', '23.8', '89.9']
  ['7', 'C', '1', '7.1', '24', '89.9']
  ['7', 'C', '2', '9999', '9111', '9333']
  ['7', 'C', '3', '9999', '9111', '9333']
  </code></pre>
  <p>What I want to have is 3 rows each having the same first item (MyNum) but their third item (Line) incrementing from 1 to 3. So if I only have 1 or 2 rows with that MyNum first term value I need to insert either one or two rows, each of which is the same as the row above it except for the Line term which should increment.</p>
  <p><strong>Desired output:</strong></p>
  <pre><code>['MyNum', 'Cycle', 'Line', 'V1', 'V2', 'T1']
  ['1', 'C', '1', '6.7', '25.6', '90']
  ['1', 'C', '2', '6.7', '25.6', '90']
  ['1', 'C', '3', '6.7', '25.6', '90']
  ['3', 'A', '1', '5.8', '22.5', '89.9']
  ['3', 'A', '2', '5.8', '24.2', '90']
  ['3', 'A', '3', '5.8', '25.4', '90']
  ['5', 'B', '1', '6', '25.3', '89.9']
  ['5', 'B', '2', '6.3', '23.8', '89.9']
  ['5', 'B', '3', '6.3', '23.8', '89.9']
  ['7', 'C', '1', '7.1', '24', '89.9']
  ['7', 'C', '2', '9999', '9111', '9333']
  ['7', 'C', '3', '9999', '9111', '9333']
  </code></pre>
  <p><strong>Code</strong></p>
  <pre><code>import csv
  data = pd.read_csv('foo.csv')
  df = pd.DataFrame(data)
  print('\n'*5)
  
  print(df[&quot;MyNum&quot;])
  &quot;&quot;&quot;
  for i in df[&quot;MyNum&quot;]:
      if i+1 = i
      print(i)
  &quot;&quot;&quot;
  with open('foo.csv', 'r') as f_in, open('__fooOut.csv', 'w') as f_out:  # this creates a new output file in write mode
      reader = csv.reader(f_in, delimiter=',') # modify for your file
      writer = csv.writer(f_out, delimiter=',') # modify for your file
      num = 0
      num_count = 3
      while num_count &gt; 0:
          for row in reader:
              print(row)
  
  
  &quot;&quot;&quot;
  Manual method
  
  The first item in the first row is 1 and a third item 1.
  The following three rows have a first item of 3 and their third items go from 
  1, 2 to 3.
  The following two rows have a first item of 5 and their third items go from 
  1 to 2.
  The following 3 rows have a first item of 7 and their third items go from 
  1, 2 to 3.
  
  What should happen is there should be three rows each having the same first 
  item, and when they do their third item (row[2] or &quot;Line&quot;) should increment and
   be either a 1, 2 or 3.
  When there is not two rows with the same first item as the row above a new row
   should be inserted immediately below the row with the same details as the 
   row above except for the third item.
  &quot;&quot;&quot;
  </code></pre>
  <p>I don't know how to do this, whether the approach should be a dataframe or not,
  nor how to check that the next row's first item is equal to the row under test.</p>