title: Running a df through a function iterating rows
body: <p>So I am trying execute a buy/sell action based on the 5 period moving average exceeding that of the 20 period moving average. After receiving too many errors I decided to do it separately for each relevant df instead of writing a function for it. An image of the final product can be seen below (last column of the screenshot is what I was hoping to achieve using one of these two functions). When I played around with these two functions I also came across the error which was 'key is invalid' which I didn't know how to solve. Any information would be much appreciated</p>
  <pre><code>def action(df):
      for i in range(df.index[]):
          if df.loc[i,'PointMovingAverage5'] &gt; df.loc[i,'PointMovingAverage20'] &amp; df.loc[i-1,'PointMovingAverage5'] &lt; df.loc[i-1,'PointMovingAverage20']:
              df.loc[i, 'BUY/SELL'] = 'BUY'
          elif df.loc[i,'PointMovingAverage5'] &lt; df.loc[i,'PointMovingAverage20'] &amp; df.loc[i-1,'PointMovingAverage5'] &gt; df.loc[i-1,'PointMovingAverage20']:
              df.loc[i, 'BUY/SELL'] = 'SELL'
          else:
              df.loc[i, 'BUY/SELL'] = 'NaN'
       
  CBA['BUY/SELL'] = CBA.apply(action, axis = 1)
  
  ERROR: 'str' object cannot be interpreted as an integer
  
  def Action(df):
      for label, row in df.iterrows():
          if (df[row,'PointMovingAverage5'] &gt; df[row,'PointMovingAverage20']) &amp; (df[row-1,'PointMovingAverage5'] &lt; df[row-1,'PointMovingAverage20']):
              df.loc[label, 'BUY/SELL'] = 'BUY'
          elif (df[row,'PointMovingAverage5'] &lt; df[row,'PointMovingAverage20']) &amp; (df[row-1,'PointMovingAverage5'] &gt; df[row-1,'PointMovingAverage20']):
              df.loc[label, 'BUY/SELL'] = 'SELL'
          else:
              df.loc[label, 'BUY/SELL'] = 'NO ACTION'
          return df
             
  CBA = CBA.apply(Action, axis = 1) 
  
  ERROR: 'Series' object has no attribute 'iterrows'
  </code></pre>
  <p><a href="https://i.stack.imgur.com/UI6kl.png" rel="nofollow noreferrer">End Goal</a></p>