title: Function to Replace outlier with Lower Limit and Upper Limit in Python
body: <pre><code>from sklearn import datasets
  import pandas as pd
  import numpy as np
  
  dt = datasets.load_diabetes()
  data = pd.DataFrame(data= np.c_[dt['data'], dt['target']],columns= 
  dt['feature_names'] + ['target'] )
  data = data.drop('sex', axis = 1)
  
  # mean +- 2sigma
  # function to calculate outlier of a variable
  def out1(x):
      mu = np.average(x)
      sigma = np.std(x)
      LL = mu - 2*sigma # Lower limit 
      UL = mu + 2*sigma # Upper limit
      out = [1 if (a &gt;= UL) | (a &lt;= LL) else 0 for a in x]
      return(out)
  
  # check #outliers in each variable
  print(data.apply(out1).apply(sum))
  
  
  # Function to Replace outlier with LL / UL
  
  def out_impute(x):
      mu = np.average(x)
      sigma = np.std(x)
      LL = mu - 2*sigma # Lower limit 
      UL = mu + 2*sigma # Upper limit
      xnew = "Enter Code Here"
      return(xnew)
  
  data1 = data.apply(out_impute) # Create new data with inputed values
  </code></pre>
  
  <p>Please someone help me with how could I replace the outliers with lower and upper limit.</p>
  
  <p>I defined outliers as values >= mu + 2*sigma and =&lt; mu - 2*sigma. I defined a function in my code 'out_impute' but I got stuck at the replacing part.</p>
  
  <p>Thanks in advance!</p>