If loop adds a row, does that new row get evaluated by the loop?

Hello all,

I am new to Python and only about 25% through the Data Scientist programme.

I am trying to put something together for work and I have a question that I hope somebody can answer.

I have a pandas dataframe that I want to loop through and carry out some functions on the data:
Take the time needed to manufacture a unit and subtract the time available from the row. Put this
into column called ‘Remaining Work’

  Use an IF statement to see if the remaining work is 0, if so conitnue to the next row. Else, add a 
  new row with the date changed to next day, Remaining work value used for time needed etc. etc. 

I can figure those parts out on my own and am most of the way there. My question is, if the the loop adds a new row to the dataframe being looped through. Will the loop then iterate through these new rows or does it only run on the rows that existed when the loop began?

Essentially what this function will do is look at a job that needs manufacturing. Take the start date and total time needed to build it. Work out how much of the job can be done on that date, then add a new row for the next day with the remaining work and repeat this until the job is complete. Therefore giving me a list of dates worked and what work will be in progress on those days so I can compare it to production capacity and see if we are over or under loaded.

If anyone reads this and thinks I am greatly over complicating the way to achieve this then I would appreciate their input as well.

Thanks All

Mark

It depends exactly how you write it. There are options to modify dataframes in-place which could affect what you’re iterating through. This is generally considered bad practice (it’s a bit unpredictable and harder to read/debug/maintain). Alternatively you can simply return a new dataframe with additional rows or just store and work on what would be each new row in turn.

So, you can alter what you’re iterating through but I would suggest avoiding that.

1 Like

I’m not sure if I understand the data structure but it seems like a while loop with a counter would accomplish this.

while remaining work
  row = 0
  if data[row] isn't full
    data[row] assignment
    remaining work adjustment
  row += 1

just a conceptual guide.

1 Like