title: Duplicating rows in dataframe python
body: <p>Good afternoon everyone,</p>
  
  <p>I am currently writing a thesis on the KMV model in python. I took inspiration from the code <a href="https://stackoverflow.com/questions/52270610/newton-method-not-converging-after-iteration-python-scipy">here</a> to solve the non-linear equations. <a href="https://drive.google.com/file/d/1b0rLYoE-RFP38gMMvk6oetbrTYTdyeen/view?usp=sharing" rel="nofollow noreferrer">Here</a> is the link to the CSV file used to create the dataframe. And this is the code I have so far:</p>
  
  <h2>Importation of the required modules</h2>
  
  <pre><code>from datetime import datetime
  import pandas as pd
  import numpy as np
  import scipy.optimize as sco
  from scipy.stats import norm
  
  
  df = pd.DataFrame()
  df = pd.read_csv("AREX.csv", sep=';', engine = "python", decimal=',')
  </code></pre>
  
  <h2>Functions to prepare the file for the model to run</h2>
  
  <pre><code>def clean():   
  #    df.rename(columns ={"ï»¿Date": "Date"}, inplace = True)
  #    df["Date"] = pd.to_datetime(df['Date'])
      df.set_index("Date", inplace = True)
      df['AREX.O']=df['AREX.O'].astype(float)
      df.drop(['Total Short Term debt'], axis =1, inplace = True)
      return df
  
  def preparation():
      df['e']=df['AREX.O']*df['Share Outstanding']
      df['Short Term Debt']=df['Debt']-df['Total Long term Debt']
      df['f']=df['Short Term Debt']+df['Total Long term Debt']*0.5
      df['log_ret'] = np.log(df['AREX.O']) - np.log(df['AREX.O'].shift(1))
  #    df['stdev']=df['log_ret'].rolling(252).std()*m.sqrt(252)
      return df
  </code></pre>
  
  <h2>Algorithm used to solve for a and sigma_a.</h2>
  
  <p>I only tried to adapt the code to my dataframe here</p>
  
  <pre><code>def algo1():
  
  #    formatting the vaules as required
      df["f"] = df["f"].astype(float)
      df["e"] = df["e"].astype(float)
  
  
  #    #computating of key input variable for the model
  
      df['a'] = df['f'].add(df["e"])
  
  
      #defining a function for the black Scholes equation
  
      def bseqn(a, debug=False):
          d1 = (np.log(a/f) + (r + 0.5*sigma_a**2)*T)/(sigma_a*np.sqrt(T))
          d2 = d1 - sigma_a*np.sqrt(T)
          y1 = e - (a*norm.cdf(d1) - np.exp(-r*T)*f*norm.cdf(d2))
  
          if debug:
              print("d1 = {:.6f}".format(d1))
              print("d2 = {:.6f}".format(d2))
              print("Error = {:.6f}".format('y1'))
  
          return y1
  
      #Solving the model
  
      time_horizon=[1]
      timesteps = range(1, len(df))
  
      results = np.empty((df.shape[0],len(time_horizon)))
  
      #looping to solve for each row
  
      for i, years in enumerate(time_horizon):
          T = 1
          results[:,i] = df.loc[:,'a'] 
          for i_t, t in enumerate(timesteps):  
              a = results[t-10:t,i]
              ra =np.log(a/np.roll(a,1))
              sigma_a = np.nanstd(ra) #gives initial value of sigma_a
  
              if i_t == 0:
                  subset_timesteps = range(t-1, t+1)
                  print(subset_timesteps)
              else:
                  subset_timesteps = [t]
  
              n_its = 0
              while n_its &lt; 10:
                  n_its += 1
                  for t_sub in subset_timesteps:
                      r = df.iloc[t_sub]['r'] 
                      f = df.iloc[t_sub]['f']
                      e = df.iloc[t_sub]['e']
                      sol = sco.fsolve(bseqn, results[t_sub,i]) #if I replace newton with fsolve the code works properly
                      results[t_sub,i] = sol # stores the new values of a 
  
                  # Update sigma_a based on new values of a
                  last_sigma_a = sigma_a
                  a = results[t-10:t,i]
                  ra = np.log(a/np.roll(a,1))
                  sigma_a = np.nanstd(ra) #new val of sigma
                  diff = last_sigma_a - sigma_a
                  if abs(diff) &lt; 1e-3:
                      df.loc[t_sub,'sigma_a'] = sigma_a
                      break 
              else:
  
                  pass
      return df
  </code></pre>
  
  <h2>Run function</h2>
  
  <pre><code>def run():   
      clean()
      preparation()
      algo1()
      print(df)
      print(list(df))
  #    main_df = df.to_csv("AREX_D.csv")
  </code></pre>
  
  <p>The output should write the results of sigma_a on the created sigma_a column but instead of that it adds a row so instead of 1500 rows i end-up with 3000 rows most of it being Nan values. I do not understand where the code asks that...</p>
  
  <p>I suspect it to come from these lines:</p>
  
  <pre><code>   diff = last_sigma_a - sigma_a
                      if abs(diff) &lt; 1e-3:
                          df.loc[t_sub,'sigma_a'] = sigma_a
                          break 
  </code></pre>
  
  <p>Does anyone has any insight on what is happening ?</p>
  
  <p>Here is a picture of the output : 
  <a href="https://i.stack.imgur.com/G76A5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G76A5.jpg" alt="The results of the sigma seemed to be moved and start from the last row instead of the samed rows as the other variables"></a></p>
  
  <p>Thank you very much! </p>