title: How to use groupby + transform instead of pipe?
body: <p>Let's say I have a dataframe like this</p>
  <pre><code>import pandas as pd
  from scipy import stats
  
  df = pd.DataFrame(
      {
          'group': list('abaab'),
          'val1': range(5),
          'val2': range(2, 7),
          'val3': range(4, 9)
      }
  )
  
    group  val1  val2  val3
  0     a     0     2     4
  1     b     1     3     5
  2     a     2     4     6
  3     a     3     5     7
  4     b     4     6     8
  </code></pre>
  <p>Now I want to calculate linear regressions for each group in column <code>group</code> using two of the <code>vali</code> columns (potentially all pairs, so I don't want to hardcode column names anywhere).</p>
  <p>A potential implementation could look like this based on <code>pipe</code></p>
  <pre><code>def do_lin_reg_pipe(df, group_col, col1, col2):
      group_names = df[group_col].unique()
      df_subsets = []
      for s in group_names:
          df_subset = df.loc[df[group_col] == s]
          x = df_subset[col1].values
          y = df_subset[col2].values
          slope, intercept, r, p, se = stats.linregress(x, y)
          df_subset = df_subset.assign(
              slope=slope,
              intercept=intercept,
              r=r,
              p=p,
              se=se
          )
          df_subsets.append(df_subset)
      return pd.concat(df_subsets)
  </code></pre>
  <p>and then I can use</p>
  <pre><code>df_linreg_pipe = (
      df.pipe(do_lin_reg_pipe, group_col='group', col1='val1', col2='val3')
        .assign(p=lambda d: d['p'].round(3))
  )
  </code></pre>
  <p>which gives the desired outcome</p>
  <pre><code>  group  val1  val2  val3  slope  intercept    r    p   se
  0     a     0     2     4    1.0        4.0  1.0  0.0  0.0
  2     a     2     4     6    1.0        4.0  1.0  0.0  0.0
  3     a     3     5     7    1.0        4.0  1.0  0.0  0.0
  1     b     1     3     5    1.0        4.0  1.0  0.0  0.0
  4     b     4     6     8    1.0        4.0  1.0  0.0  0.0
  </code></pre>
  <p>What I don't like is that I have to loop through the groups, use and <code>append</code> and then also <code>concat</code>, so I thought I should somehow use a <code>groupby</code> and <code>transform</code> but I don't get this to work. The function call should be something like</p>
  <pre><code>df_linreg_transform = df.copy()
  df_linreg_transform[['slope', 'intercept', 'r', 'p', 'se']] = (
      df.groupby('group').transform(do_lin_reg_transform, col1='val1', col2='val3')
  )
  </code></pre>
  <p>question is how to define <code>do_lin_reg_transform</code>; I would like to have something along these lines</p>
  <pre><code>def do_lin_reg_transform(df, col1, col2):
      
      x = df[col1].values
      y = df[col2].values
      slope, intercept, r, p, se = stats.linregress(x, y)
  
      return (slope, intercept, r, p, se)
  </code></pre>
  <p>but that - of course - crashes with a <code>KeyError</code></p>
  <blockquote>
  <p>KeyError: 'val1'</p>
  </blockquote>
  <p>How could one implement <code>do_lin_reg_transform</code> to make it work with <code>groupby</code> and <code>transform</code>?</p>