For loop to compare previous values in python
<p>I have a pandas dataframe like this.. </p>

<pre><code> order_id buyer_id scheduled_order  minutes   flag  
  525      232               1        13      Null   
  862      232               1        14      Null   
 1361      232               1        15      Null   
 1373      232               1        13      Null   
 1580      232               1        14      Null   
 1729      232               0        11      Null   
 1817      232               1        18      Null  
</code></pre>

<p>I want to set a flag depending upon value of <code>scheduled_order</code>.
If first order is scheduled order(<code>scheduled_order =1</code>) flag should set to <code>0</code>, else it should check if minutes are greater than 12 then flag should be 1 else 2 Then, for next order if previous order is scheduled order then, flag should set to 3. If previous order is live order(<code>scheduled_order =0</code>) and if minutes is less than 12 then flag should set to 2. if minute is greater than 12 then flag should set to 1.</p>

<p>My desired output is</p>

<pre><code>order_id buyer_id scheduled_order  minutes   flag  
  525      232               1        13      0   
  862      232               1        14      3   
 1361      232               1        15      3   
 1373      232               1        13      3   
 1580      232               1        14      3   
 1729      232               0        11      3   
 1817      232               1        18      2
</code></pre>

<p>Here is my code in python</p>

<pre><code>for i in range(len(df)):
    if(df.scheduled_order[i]  == '1'):
            speed.flag[i] = '0'
    else:
        if(minutes &gt; 12):
            df.flag[i] = '1'
        else:
            df.flag[i] = '2'
</code></pre>

<p>But when <code>i</code> becomes 1 how do I check for previous <code>scheduled_order</code> value?</p>