title: Perform multiple linear regression for groups based on column unique values
body: <p>I need to perform multiple linear regression for 4 different groups which are taken from column <code>df['status']</code> with <code>df['status'].unique()</code> values of (1, 4, 7, 9). After regression I need to save the results in new column  <code>df['reg_results']</code>.</p>
  <p>Example of data:</p>
  <pre><code>Out[71]: 
      ID  status    y_Values     a   b      c      d
  0    1       1  150.510000  0.26  23  0.151  1.215
  1    2       1  153.110000  0.86  14  0.156  1.651
  2    3       1  189.320000  0.46  51  0.151  2.154
  3    4       9  145.650000  0.46  62  0.157  3.145
  4    5       4  189.650000  0.91  11  0.123  2.104
  5    6       4  144.230000  0.69  16  0.178  3.515
  6    7       4  198.020000  0.62  18  0.891  1.561
  7    8       9  178.090000  0.91  22  0.156  9.155
  </code></pre>
  <p>The columns needed in regression are <code>X = ['a', 'b', 'c', 'd']</code> and <code>y = ['y_Values']</code>.</p>
  <p>I have found multiple solutions where the regression is performed using the whole column or columns, like:</p>
  <pre><code>data = pd.read_csv(r'E:\...\data.csv')
  lm = smf.ols(formula='y_Values ~ a + b + c + d', data=data).fit()
  print(lm.params)
  </code></pre>
  <p>with the result:</p>
  <pre><code>Intercept   -403.803691
  a              0.170452
  b             40.866943
  c             14.839920
  d              1.618234
  dtype: float64
  </code></pre>
  <p>However, I would like to do the same for each <code>df['status'] == (1,4,7,9)</code> rows. And store the data in new column.</p>
  <p>I know how to do this in R, but cannot understand how to add these  <code>df['status']</code> parameters in analysis:</p>
  <pre><code>lapply(c(1,4,7,9), function(k){
  
    data &lt;- shape[status == k, c(&quot;ID&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;y_Values&quot;)]
    reg &lt;- lm(y_Values ~ a + 0 + b + c + d, data = data)
    reg2 &lt;- step(reg, direction = &quot;backward&quot;)
  </code></pre>