code: def average_repetitions(df, keys_mean):
      
      if 'norrec' not in df.columns:
          raise Exception(
              'The "norrec" column is required for this function to work!'
          )
  
      # Get column order to restore later
      cols = list(df.columns.values)
  
      keys_keep = list(set(df.columns.tolist()) - set(keys_mean))
      agg_dict = {x: _first for x in keys_keep}
      agg_dict.update({x: np.mean for x in keys_mean})
      for key in ('id', 'timestep', 'frequency', 'norrec'):
          if key in agg_dict:
              del(agg_dict[key])
      # print(agg_dict)
  
      # average over duplicate measurements
      extra_dimensions_raw = ['id', 'norrec', 'frequency', 'timestep']
      extra_dimensions = [x for x in extra_dimensions_raw if x in df.columns]
      df = df.groupby(extra_dimensions).agg(agg_dict)
      df.reset_index(inplace=True)
      return df[cols]
docs: average duplicate measurements. This requires that IDs and norrec labels
      were assigned using the *assign_norrec_to_df* function.
  
      Parameters
      ----------
      df
          DataFrame
      keys_mean: list
          list of keys to average. For all other keys the first entry will be
          used.