title: Applying custom function on groupby on large data set
body: <p>I have a very large dataframe (millions of rows) and want to apply a custom function on a groupby.
  The dataframe is ordered by &quot;A&quot; as well as a date column &quot;C&quot;. The function returns a new vector for &quot;C&quot; which should later replace the original column &quot;C&quot;.</p>
  <p>This is what I'm doing:</p>
  <pre><code>result = df.groupby(&quot;A&quot;, sort=False).apply(lambda x: myFunction(x[&quot;B&quot;], x[&quot;C&quot;], x[&quot;D&quot;])
  </code></pre>
  <p>The function itself takes three columns as input arguments, does some order-sensitive if-else calculations and returns &quot;C&quot; with possibly changed values.</p>
  <pre><code>def myFunction(B, C, D):
      df = pd.concat([B, C, D], axis=1).reset_index(drop=True)
      
      ... some if-else calculations ...
  
      return(df[&quot;C&quot;])
  </code></pre>
  <p>Now this calculation takes many hours if not days to complete on the whole dataset.</p>
  <p>Also, <code>result</code> is now a dataframe where each row is the result of one groupby-group.
  However i would want the result to be one continues vector where the newly returned &quot;C&quot; columns replace the column &quot;C&quot; in the original dataframe.</p>
  <p>I'm pretty sure there is a way better way to do this but I can't seem to find it.</p>
  <p>The whole thing was originally programmed in R with dplyr and was running way quicker.</p>
  <pre><code>df &lt;- df %&gt;% group_by(A) %&gt;%
    mutate(C= myFunction(B,C,D))
  </code></pre>