title: Pandas:Fill null values with last available values and a flag
body: <p>I am looking for a logic to produce an output/update values in value column based on flag Y. Notice the second N in bold. We won’t be filling values for next two Ys since the last value is N and it’s null. If N has a value we can ffill next Y row.</p>
  <p>I have tried using df_latest.loc[(df_latest['flag'] == 'Y'), ‘value’] =df_latest[‘value’].fillna(method='ffill')
  This logic doesn't cover the scenario when N is null and it forward fills all the preceding the NUll row.</p>
  <pre><code>flag    value   new_val
  
  Y           1       1 
  
  Y           2       2
  
  Y           NaN     2
  
  N           3       3
  
  Y           NaN     3
  
  Y           5       5
  
  N           NaN     NaN
  
  Y           NaN     NaN
  
  Y           NaN     NaN
  
  N           6       6
  
  Y           NaN     6
  
  Y           NaN     6
  
  Y           NaN     6
  
  Y           NaN     6
  
  Y           NaN     6
  </code></pre>
  <p><a href="https://i.stack.imgur.com/vIL2a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vIL2a.png" alt="enter image description here" /></a></p>