title: iterrate over dataframe and based on the value of one column do operations in a new column with previous row's value
body: <p>I have small df of stock prices with their actions. I would like to calculate the adjust ownership amount of a stock after the split (i.e. if you own a 1000 share and stock have 2-1 split then your ownership become 2000 share). I would like to iterrate over &quot;Stock Splits&quot; column and if the value != 0 then multiply &quot;ownership&quot; with &quot;Stock Splits&quot; otherwise maintain the last quantity before the split. I tried many methods, but I am not sure where I am going wrong - i do think the logic is wrong but don't know how to fix it.</p>
  <pre><code>import yfinance as yf
  aapl = yf.Ticker(&quot;AAPL&quot;)
  hist = aapl.history(start=&quot;2014-06-01&quot;)
  hist[&quot;ownership&quot;] = 1000
  
  
      Open    High    Low Close   Volume  Dividends   Stock Splits    ownership
  Date                                
  2014-06-02  20.338966   20.366877   19.971301   20.168608   369350800   0.0 0.0 1000
  2014-06-03  20.162511   20.492319   20.155774   20.453819   292709200   0.0 0.0 1000
  2014-06-04  20.450610   20.785872   20.407940   20.687378   335482000   0.0 0.0 1000
  2014-06-05  20.731655   20.833356   20.616479   20.768549   303805600   0.0 0.0 1000
  2014-06-06  20.850357   20.893990   20.676150   20.711439   349938400   0.0 0.0 1000 
  </code></pre>
  <p>my codes is as follow:</p>
  <pre><code> hist.loc[hist['Stock Splits']==0,'ownerAdj'] = hist['ownership'].shift(1)
  hist.loc[hist['Stock Splits']!=0,'ownerAdj'] = hist['ownership'].shift(1) * hist['Stock Splits']
  </code></pre>
  <p>However I am not always getting correct figures, like in the below example, in 2014-06-09 aapl had split (7 to 1) so the results should be 7000 from 2014-06-09 until the next date it has another split which is 2020-08-31 but I am getting back the 1000 after the split</p>
  <pre><code>Date    Open    High    Low Close   Volume  Dividends   Stock Splits    ownership   ownerAdj
  0   2014-06-02  20.338964   20.366875   19.971299   20.168606   369350800   0.0 0.0 1000    NaN
  1   2014-06-03  20.162515   20.492323   20.155778   20.453823   292709200   0.0 0.0 1000    1000.0
  2   2014-06-04  20.450608   20.785870   20.407938   20.687376   335482000   0.0 0.0 1000    1000.0
  3   2014-06-05  20.731645   20.833346   20.616470   20.768539   303805600   0.0 0.0 1000    1000.0
  4   2014-06-06  20.850359   20.893992   20.676152   20.711441   349938400   0.0 0.0 1000    1000.0
  5   2014-06-09  20.818268   21.083269   20.604921   21.042845   301660000   0.0 7.0 1000    7000.0
  6   2014-06-10  21.274162   21.346027   21.013652   21.166365   251108000   0.0 0.0 1000    1000.0
  7   2014-06-11  21.139424   21.280908   20.991204   21.078789   182724000   0.0 0.0 1000    1000.0
  </code></pre>
  <p>I tried to run loop but I am getting error:</p>
  <pre><code>for i, row in hist.iterrows():
      if row[&quot;Stock Splits&quot;] == 0:
          row[&quot;ownerAdj&quot;] = row[&quot;ownership&quot;].shift(1)
      elif row[&quot;Stock Splits&quot;] != 0:
          row[&quot;ownerAdj&quot;] = row[&quot;ownership&quot;].shift(1) * row[&quot;Stock Splits&quot;]
  
   ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-51-2d94c5e86953&gt; in &lt;module&gt;
        1 for i, row in hist.iterrows():
        2     if row[&quot;Stock Splits&quot;] == 0:
  ----&gt; 3         row[&quot;adjust2&quot;] = row[&quot;ownership&quot;].shift(1)
        4     elif row[&quot;Stock Splits&quot;] != 0:
        5         row[&quot;adjust2&quot;] = row[&quot;ownership&quot;].shift(1) * row[&quot;Stock Splits&quot;]
  
  AttributeError: 'numpy.float64' object has no attribute 'shift'
  </code></pre>