title: How to do the mean relative error for different groups after groupby?
body: <p>I am doing a project that deals with a large dataset. However, now I wanted to make a mere comparison and I can't find how I can do it. Simply put to a simpler problem, my problem is as follows. I have 3 different columns, and 6 rows:</p>
  
  <pre><code>A   B   C
  g   1   2 
  h   2   1
  j   3   3
  h   3   3
  g   4   3
  j   5   4
  </code></pre>
  
  <p>The first thing I wanted to do was calculate B's average, median and mode taking into account A's group. So I did this way, and so far so good:</p>
  
  <pre><code>aggregations_proc = {'C': { 'Mean_proc': 'mean', 'Median_proc': 'median',
                                    'Mode_proc': lambda x: x.mode()}}
  groupy_proc = df.groupby('A', as_index=False).agg(aggregations_proc)
  </code></pre>
  
  <p>However, my goal now was simply to create a new column representing the calculation of the mean relative error for each of the rows between B and the mean of C, given that the mean varies for each group.
  For example, for the first line, the relative error would be:
  (Group g average - 1st row B value) / Group g average
  However, for the second line, it would already be
  (Group h average - 2nd row B value) / Group h average
  And for the 3rd line
  (Group j average - 3rd row B value) / Group j average
  But this applies to a much larger dataset. </p>
  
  <p>Any help will be very welcome!</p>
  
  <p>[UPDATE]</p>
  
  <p>Here's my new code. I think everything is working, but I guess theres some error about the mode. The error is " Columns must be same length as key". The code is the following:</p>
  
  <pre><code>aggregations_diag = ('mean', 'median', lambda x: x.mode().iloc[0])
  groupy_diag = df.groupby('A', as_index=False) 
  ['B'].agg(aggregations_diag) 
  
  def absolute_error_diag(x): 
  stats = ['mean', 'median', lambda x: x.mode().iloc[0]]
  avg = groupy_diag.loc[x['A'], stats] 
  return (avg - x['B'])
  
  # Columns for the absolute error of each row
  columns_names_diag = ['ae_diag_mean','ae_diag_median','ae_diag_mode']
  
  df[columns_names_diag] = df.apply(absolute_error_diag, axis=1)
  
  [MAE_diag_mean, MAE_diag_median, MAE_diag_mode] = 
  df[columns_names_diag].sum()/len(df['ae_diag_mean'])
  </code></pre>