title: Pandas Create New Column Based on Value in Another Column, If False Return Previous Value of New Column
body: <p>this is a Python pandas problem I've been struggling with for a while now.  Lets say I have a simple dataframe df where df['a'] = [1,2,3,1,4,6] and df['b'] = [10,20,30,40,50,60].  I would like to create a third column 'c', where if the value of df['a'] == 1, df['c'] = df['b'].  If this is false, df['c'] = the previous value of df['c'].  I have tried using np.where to make this happen, but the result is not what I was expecting.  Any advice?</p>
  
  <pre><code>df = pd.DataFrame()
  df['a'] = [1,2,3,1,4,6]
  df['b'] = [10,20,30,40,50,60]
  df['c'] = np.nan
  df['c'] = np.where(df['a'] == 1, df['b'], df['c'].shift(1))
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>   a   b     c
  0  1  10  10.0
  1  2  20   NaN
  2  3  30   NaN
  3  1  40  40.0
  4  4  50   NaN
  5  6  60   NaN
  </code></pre>
  
  <p>Whereas I would have expected:</p>
  
  <pre><code>   a   b     c
  0  1  10  10.0
  1  2  20  10.0
  2  3  30  10.0
  3  1  40  40.0
  4  4  50  40.0
  5  6  60  40.0
  </code></pre>