title: In a Python Function, write the pandas result to a CSV
body: <p><a href="https://i.stack.imgur.com/AfBwe.jpg" rel="nofollow noreferrer">enter image description here</a>Hello I am trying to write a script in which I look at some historical data (Stock Prices) and compute a rolling Linear Regression Slope.</p>
  <p>I have a CSV file with the Close Price of AAPL US Equity from 01-01-2010 - 10-23-2020.</p>
  <p>As you can see in the below code I have read the CSV, created a few new variables, The Moving Linear Slope etc...  and wrote that data into the CSV as new columns. PS: ROC is the Rate of Change of the MLR Slope.</p>
  <p>I am now trying to create a function which returns 1,0,-1 (1=Long, 0 = No Position, -1=Short) for each date/stock price. I have created the below function &quot;scorer&quot;  but am unable to figure out how to get it to look at each date and write the corresponding value into a new column in the CSV.</p>
  <p>Any help would be greatly appreciated. Thanks in advance.</p>
  <pre><code>import pandas as pd
  import talib as ta
  import matplotlib.pyplot as plt
  
  data = pd.read_csv(&quot;Copper.csv&quot;)
  score = 0
  
  mlrs = ta.LINEARREG_SLOPE(data[&quot;Close&quot;].values,14)*-1
  sma = data.MlrSlope.rolling(14).mean()
  roc = data.MlrSlope.rolling(5).mean()
  
  #Create new columns for Roc and SMA. We then Write the new Column data to the CSV file. This should add both the ROCC and SMA Columns. 
  data[&quot;Rocc&quot;] = roc
  data[&quot;SMA&quot;] = sma
  data.to_csv(&quot;copper.csv&quot;,index=True)
  
  # The below function looks to check for the SMA &amp; MLR Slope being Positive... ***JOSHUA BEFORE FINISHING*** add another parameter that looks at the ROC of the MLR Slope... This would be my &quot;roc&quot; variable. If the change is very negative very quick it could resemble an exit opportunity on the Long Side, while a sharp Up Turn could indicate a Buy Opportunity.  
  def scorer(data):
      if(data[&quot;sma&quot;] &gt; 0 and data[&quot;MlrSlope&quot;] &gt;0 ):
              return 1
          elif (data[&quot;sma&quot;] &gt;0 and data[&quot;MlrSlope&quot;] &lt;0):
              return 0
          elif(data[&quot;sma&quot;] &lt;0 and data[&quot;MlrSlope&quot;] &gt;0):
              return 0
          elif (data[&quot;sma&quot;] &lt;0 and data[&quot;MlrSlope&quot;] &lt;0):
              return -1
  </code></pre>